Welcome to WebmasterWorld Guest from 54.196.73.22

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

.htaccess rewrite/redirect problem (case is changing)

     
2:35 am on Mar 27, 2007 (gmt 0)

New User

10+ Year Member

joined:Mar 27, 2007
posts:4
votes: 0


I have a website (Apache 1.3.37 on Linux) which has four domains pointed to it (olddomain1.com, olddomain2.com, olddomain3.com and newdomain.com). I am trying to modify the .htaccess file so that whenever someone browses to it using the old domain names they are (permanently) redirected to the same page but using the new domain name.

For example when someone requests
[olddomain2.com...]
they should be redirected (permanently) to
[newdomain.com...]

This is what I have in the .htaccess file:

Options All -Indexes +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.olddomain1\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^olddomain1\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.olddomain2\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^olddomain2\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.olddomain3\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^olddomain3\.com$ [NC,OR]
RewriteRule (.*) [newdomain.com...] [R=301,L]

My problem is, when I browse to a web page the domain name replacement happens but the path and file name of the page is transformed into lowercase. Naturally this doesn't work as the web server is case sensitive.

For example, when I browse to:
[olddomain2.com...]
I receive:
[newdomain.com...]

Could someone please help me to correct the .htaccess file so that I have permanent redirections from the old domain names but preserves the case of the path and filename of the web pages so that they display?

Thank you for your time.

3:05 am on Mar 27, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


The only thing wrong with your file is that the last RewriteCond "in a stack" *must never* have an [OR] on it.

For the case-changing problem, I'd be looking elsewhere if fixing this doesn't help.

Also, you can shorten that code up quite a bit by making the "www." optional:


RewriteCond %{HTTP_HOST} ^(www\.)?olddomain1\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain2\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain3\.com [NC]
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]

Get rid of the end-anchors on the domain patterns -- Otherwise, if a port number is appended, the rule will fail. Or you can actually check for this. Example: ^(www\.)?olddomain1\.com(:[0-9]{1,5})?$ [NC,OR]

If you want to further reduce the size of the code, you can use the "local OR". But be aware that the solid pipe character used to indicate a local OR is modified by posting on this forum; So change the broken pipes "¦" to solid pipes before use. You should also canonicalize the new domain as "www":


RewriteCond %{HTTP_HOST} ^(www\.)?(olddomain1Šolddomain2Šolddomain3)\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^newdomain\.com [NC]
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]

Jim
5:16 am on Mar 27, 2007 (gmt 0)

New User

10+ Year Member

joined:Mar 27, 2007
posts:4
votes: 0


Thank you very much for your quick response! Things start to work when I take out the last OR.

I couldn't get your last two suggestions to work for me. (Yes, I did replace the pipe symbol.) It seems to send the browser into a loop - I tested with IE7 and Firefox 2.0.0.2. After a while Firefox reports the following error:

"The page isn't redirecting properly

Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
* This problem can sometimes be caused by disabling or refusing to accept cookies."

I'm not too worried though - the longer version is doing the trick. Thank you so much for your time.

12:30 pm on Mar 27, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


> It seems to send the browser into a loop

That would imply one of three problems: Either a bad (or very old) POSIX regular-expressions library install on your machine, an error in transliterating the "olddomain/newdomain" strings to the actual host names, or an unneeded "correction" of the code that broke it (The code is simple, well-known, and correct as originally posted).

Because the first of these possibilities would imply a serious current problem and the potential for much trouble in the future, I'd advise checking it out...

BTW, the "Live HTTP Headers" Mozilla/Firefox extension is a great tool for testing redirects.

Jim

12:16 am on Mar 28, 2007 (gmt 0)

New User

10+ Year Member

joined:Mar 27, 2007
posts:4
votes: 0


Jim, thank you very much for your help. I appreciate it a great deal. As you can probably tell I am new to this sort of thing.

I do not have full admin access to the web server that I am working with. Only to the directory that contains our web site. The server belongs to and is administered by the company that hosts our web site. I will certainly be contacting them about the point you raised regarding the POSIX library.

I am sure that I got the domain names correct in the the .htaccess files I created following your suggestions above. I checked them very closely. I will be testing them again, this time using the "Live HTTP Headers" add-on you suggested. Thanks for the tip.

But it is working now. Thank you very much.

Jac

2:36 am on Mar 28, 2007 (gmt 0)

New User

10+ Year Member

joined:Mar 27, 2007
posts:4
votes: 0


> an unneeded "correction" of the code that broke it

Jim, I've had another look at what I had done. You're absolutely correct I did mess it up. I "simplified" my old domain names in my original post. It would have been more accurate to list my old domain names as olddomain.com, olddomain.co.uk and olddomain.co.za, and the new domain name as olddomain.com.au. So when I modified your second suggestion above I ended up with:

RewriteCond %{HTTP_HOST} ^(www\.)?(olddomain\.comŠolddomain\.co\.ukŠolddomain\.co\.za) [NC,OR]
RewriteCond %{HTTP_HOST} ^olddomain\.com\.au [NC]
RewriteRule (.*) [olddomain.com.au...] [R=301,L]

Of course the the loop occurred because of the first argument in the first RewriteCond matched also olddomain.com.au. When I added the part to check also for a port number at the end of the condition it worked perfectly. ie (:[0-9]{1,5})?$

Thank you very much for your help.