.htaccess re-write – redirect https IP to domain/sub-domain

8 Comments

.htaccess re-write IP to domain

Today we engage the tedious task of redirecting ip’s, canonical, and https subdomains case by case;

First we need to take care of the canonical’s by writing to our .htaccess file using Purple Turtle Productions as the example:

This .htaccess rewrite will take the non-www , non formatted urls, or anything accessing port 80 and clean them so they all go to www.montreal-web-design.ca

RewriteEngine on 
RewriteCond %{HTTP_HOST} ^montreal-web-design.ca$ [NC] 
RewriteCond %{SERVER_PORT} ^80$ 
RewriteRule ^(.*)$ http://www.montreal-web-design.ca/$1 [R=301,L]

Next we take our server ip and force a domain resolution to it.
This .htaccess re-write takes http://216.172.179.134 and turns it to http://www.montreal-web-design.ca

RewriteEngine on
RewriteCond %{HTTP_HOST} ^216.172.179.134$ [NC]
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^(.*)$ http://www.montreal-web-design.ca/$1 [R=301,L]

The Third and final redirect it the ever-elusive https://your-ip and resolving it to a https://purpleturtle.pro or https whatever you want.
This .htaccess re-write takes https://216.172.179.134 and turns it to http://purpleturtle.pro

RewriteEngine on
RewriteCond %{HTTP_HOST}/s%{HTTPS} ^(216.172.179.134)/((s)on|s.*)$ [NC]
RewriteCond %{SERVER_PORT} ^443$
RewriteRule ^(.*)$ http://purpleturtle.pro/$1 [R=301,L]

If you have ever tried any of these you know the first one is easy,. the second.. not so easy.. and the third you can thank me later 🙂

Formatted if you would like all three:

#This part goes in your webservers public_html
RewriteEngine on 
RewriteCond %{HTTP_HOST} ^montreal-web-design.ca$ [NC] 
RewriteCond %{SERVER_PORT} ^80$ 
RewriteCond %{HTTP_HOST} ^216.172.179.134$ [NC]
RewriteRule ^(.*)$ http://www.montreal-web-design.ca/$1 [R=301,L]

#This part goes in folder your using as your SSL sub-domain
RewriteEngine on
RewriteCond %{HTTP_HOST}/s%{HTTPS} ^(216.172.179.134)/((s)on|s.*)$ [NC]
RewriteCond %{SERVER_PORT} ^443$
RewriteRule ^(.*)$ http://purpleturtle.pro/$1 [R=301,L]

The .htaccess should be backed up before you do anything


Our Promotion and Aggregation Team are featured content delivery sources in the SEO and Web Promotion space. We scour the internet for useful search, wordpress and google news and information, and have written over 200 articles about seo and wordpress. We define the term "Living Life to the Fullest" and as such, should be your primary source for information, tips and advice in the Wordpress and Search Engine Optimization Space

About us and this blog

We are a digital marketing company with a focus on helping our customers achieve great results across several key areas.

Request a free quote

We offer professional SEO services that help websites increase their organic search score drastically in order to compete for the highest rankings even when it comes to highly competitive keywords.

Subscribe to our newsletter!

Fields marked with an * are required

More from our blog

See all posts
8 Comments
  1. Great post

    • lucinda
    • November 29, 2011

    Thnkx for spending the time to post this, It’s extremely useful for me. Big thumb up for this post!

  2. thank you

  3. Awesome things here. I am very glad to peer your post. Thank you so much and I’m taking a look ahead to contact you. Will you kindly drop me a e-mail?

  4. I just would like to give a huge thumbs up for the great info you have here on this post. I gave up about 2 months ago and of course when your not looking for something it always shows up. I tried the third combination of all three redirects and it doesn’t seem to work, if i post them individually as stated at the top it works fine. Resolving your IP do a domain is crucial in my opinion to thwart DDOS attacks since every packet will get redirected through your CDN or Cloud Service.

    Thanks Again!

      • Simon
      • November 17, 2011

      i just tested it and your right! lol i think it may have been the order i wrote it in but whenever i can.- i always try to solve as many problems with as little code as necessary.

      Thanks for pointing that out
      Simon

      i decided to leave it there .. just cause 😉

  5. Very handy info. Thanks Simon 🙂

    • Royce
    • November 15, 2011

    Hey Thanks! ill add this to my htaccess toolbox. you should post your whole htaccess file! i wanna see what yours looks like!

 

Leave a Comment