Forum Moderators: phranque

Message Too Old, No Replies

redirect to addon domain on same server

         

sheppa

12:22 pm on Mar 25, 2007 (gmt 0)

10+ Year Member



I am switching to one of my addon domains and need the old domain to redirect to the new one. I've tried this:

RewriteEngine On
RewriteRule . [newdomain.com%{REQUEST_URI}...] [R=301,L]

but it doesn't work properly. it says the page isn't redirecting properly.

If the user types in www.olddomain.com/subdir/file.php then I want it to redirect to www.newdomain.com/subdir/file.php. I want that to happen for every olddomain request.

Any ideas?

jdMorgan

2:52 pm on Mar 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'll need to add two things to stop the "not redirecting properly" problem; This error message is actually reporting an 'infinite' redirection loop.

First, check %{HTTP_HOST} to make sure there is a non-blank Host: header in the client HTTP request. Then, if present, check that Host header to be sure you have not already redirected to "www.newdomain.com". If the Host header is non-blank, and indicates a request for a domain other than "www.newdomain.com", then do the redirect.


RewriteEngine on
#
# if non-blank host header
RewriteCond %{HTTP_HOST} .
# and requested domain is NOT www.newdomain.com
RewriteCond %{HTTP_HOST} [b]![/b]^www\.newdomain\.com
# redirect to requested page on www.newdoamin.com
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]

It is often very helpful to check the operation of your new redirect code using the "Live HTTP Headers" extension to Firefox, or something similar. This can save you an awful lot of grief if you make a tiny mistake in your code -- It might save you from destroying your search engine rankings, for example. The problem discussed here is quite obvious when looking at the HTTP request/response headers -- You'll see many requests followed by redirect responses specifying the same URL as was requested, until finally the client (browser) or the server reaches it's set redirection limit.

Jim

sheppa

12:53 pm on Mar 26, 2007 (gmt 0)

10+ Year Member



Thanks a bunch! It's almost 100% working correctly, but there's one problem. When I do a direct request or want to redirect to the homepage, it does the following:

[newdomain.com...]

instead of just [newdomain.com...]

How can I fix this?

jdMorgan

1:04 pm on Mar 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'll need to find the other code that is causing this, as it is not caused by the code above. Rather, some other code is either doing this, or the new code and some pre-existing code are interfering with each other.

Basically, if another bit of code internally rewrites requests for, say, newdomain.example.com, to example.com/newdomain, and THEN you execute the new redirect code, then the redirect will 'expose' the internally-rewritten URL as you have experienced.

It is necessary to do the domain canonicalization redirects first, before doing your internal rewrites.

This is likely the result of the way your "addon" was created -- I can't speak to that, as there are various hosting company and "control panel" methods -- all different, some correct, and a few that are easy to work with... :)

Jim