Forum Moderators: phranque

Message Too Old, No Replies

301 redirect IP address to domain name

         

ichthyous

9:18 pm on Jan 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Not sure how it hapened, but Google has indexed my site both by IP address and domain name. Everything indexed by IP is in supplementals. I am trying to 301 redirect from the IP addy to the domain name but it's not working at all:

RewriteCond %{HTTP_HOST} .
# If requested domain is the IP address
RewriteCond %{HTTP_HOST}!123\.34\.678\.910
# redirect to requested page in canonical domain
RewriteRule (.*) http://example.com/$1 [R=301,L]

Here's what I currently have in place:

RewriteCond %{HTTP_HOST} .
# And if requested domain is NOT the canonical domain
RewriteCond %{HTTP_HOST}!^example\.com
# redirect to requested page in canonical domain
RewriteRule (.*) http://example.com/$1 [R=301,L]

I also see that vps.example.com is also not redirecting to the canonical domain...Am I missing something? Thanks

jdMorgan

12:45 am on Feb 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The code you have in place (the second snippet above) should redirect access by IP as well.

The new code you posted has a logic error in it, and won't work; In mod_rewrite "!" means NOT.

I don't know what the special significance of "vps.example.com" might be. However, one thing to bear in mind is that only requests which access the directory in which your .htaccess file(s) resides (or subdirectories of that directory) can be affected by the code in your .htaccess file(s). So if a request by IP address or a request to "vps" does not load the same home page and/or robots.txt file as a request to the canonical domain, then your redirect cannot work.

Jim

ichthyous

5:20 pm on Feb 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I inquired with my hosting company and they told me that to be able to redirect the IP address and the vps. subdomain I have to modify the Apache config file...I assume that to mean httpd.conf. I asked them more specifics and they didn't know. Would you happen to know more about this JD? I am on a VPS plan obviously, and I have root acces to configure httpd.conf however I want. Thanks

jdMorgan

5:52 pm on Feb 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Put the redirect within the <VirtualHost> container for each non-canonical domain. Note that for use in server config files, the rule needs to be changed slightly:

# redirect to requested page in canonical domain
RewriteRule ^/(.*)$ http://example.com/$1 [R=301,L]

Also, since the VirtualHost container limits the redirect's action to the domain specified by that container, there is no need to qualify the rule with the "RewriteCond %{HTTP_HOST}" line.

Jim

ichthyous

7:28 pm on Feb 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I sent your instructions to my hosting company's TS and they have done it. The IP and subdomain are now redirecting to the domain name as desired, however I am getting a 302 found response from the server, not a 301 permanent redirect:

#1 Server Response: [123.45.67.890...]
HTTP Status Code: HTTP/1.1 302 Found
Date: Thu, 01 Feb 2007 19:21:38 GMT
Server: Apache/1.3.37 (Unix) mod_auth_passthrough/1.8 mod_log_bytes/1.2 mod_bwlimited/1.4 PHP/4.4.4 FrontPage/5.0.2.2635.SR1.2 mod_ssl/2.8.28 OpenSSL/0.9.7a
Location: http://example.com
Connection: close
Content-Type: text/html; charset=iso-8859-1
Redirect Target: http://example.com

#2 Server Response: http://example.com
HTTP Status Code: HTTP/1.1 200 OK
Date: Thu, 01 Feb 2007 19:21:38 GMT
Server: Apache/1.3.37 (Unix) mod_auth_passthrough/1.8 mod_log_bytes/1.2 mod_bwlimited/1.4 PHP/4.4.4 FrontPage/5.0.2.2635.SR1.2 mod_ssl/2.8.28 OpenSSL/0.9.7a
X-Powered-By: PHP/4.4.4
Connection: close
Content-Type: text/html; charset=UTF-8

The same is true for the subdomain. Shouldn't these be returning a 301, not 302? thanks!

jdMorgan

1:45 pm on Feb 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In that case, it sounds like your hosts didn't follow the instructions, then.

Really, they're the only ones who can answer your questions about work they've done...

Jim