Forum Moderators: phranque
I have a site
http://www.example.com/
and a second site with the same name just a country suffix:
http://www.example.com.au/
http://www.example.com.au/ is 301 redirected to http://www.example.com/ by means of this htaccess (fragment):
# Permanent redirect for www.example.com.au
# redirect http://www.example.com.au to http://www.example.com/
RewriteCond %{HTTP_HOST} ^www\.example\.com\.au [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
It was not always this way, this htaccess change has been up about a week. The sites were previously parked on top of each other (so they could login to .au even though it was "really" the .com site)
I've tested exhaustively on Windows XP SP3, browsers FF3.0.11, IE7, IE8, Opera9.64, and Safari 4.0. All work perfectly and I can login and logout (my site uses PHP and cookies to manage the sessions).
However when a Safari 4.0 user on Mac goes to
http://www.example.com.au/ (which still shows up in Google if they google me - hasn't revisited the page yet)
they get a message saying
"Too many redirects occurred trying to open “http://www.example.com.au/”. This might occur if you open a page that is redirected to open another page which then is redirected to open the original page."
The user states:
When Safari hits the URL www.example.com or www.example.com.au it goes to http://www.example.com/login-page and then it rediverts eventually back to www.example.com.au
This user had previously logged in prior to the htaccess redirect being in place, but session cookies are set to expire after about 60 minutes (I think, might need to check this). I'm not sure if they logged into the .au or .com version of the site.
I'm at a loss what to do now, it works on everything except Safari on Mac.
Any advice much appreciated.
M
However, be aware, that as coded your rule does not provide a fix for all non-canonical requests that could be thrown at it. What does your rule do for example.com.au/ requests? It apparently does not fix those at all.
What does your rule do if any non-www or any .com request has an allowed appended port number or trailing period? Again, it doesn't address those.
If you want to redirect *all* non-canonical host names served by this folder on this server, use this code:
# Redirect host name not exactly "www.example.com" to www.example.com (also fixes requests with appended port numbers, etc)
RewriteCond %{HTTP_HOST} [b]!^[/b]www\.example\.com[b]$[/b]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
However I think you might have misunderstood my problem; it isn't a problem with multiple redirects, it works fine on all browsers except Safari. Using Live HTTP headers in Firefox just confirms the redirection is working correctly there ;)
I've tracked the problem to a restriction in Safari on domain redirections. If a user has a cookie from www.example.com.au, Safari will not allow a redirection to www.example.com. The fix is to clear (Safari) cookies, and never implement redirects to a different domain that require cookies. It is also an issue when users use hotmail with Safari, as the "remember me" function gives similar problematic behaviour.
Thanks for the canonical redirection correction!
Mark
# And for a site running on port 80
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://www.example.com/$1 [L,R]
My questions please:
1. Why the second line to "not match anything"?
2. Why is there no 301 redirection number?
3. What is the NC and L for?
Thanks
Mark
2. The =301 is missing, so you get a 302 redirect. This is not a searchengine friendly example of code. The R means 'redirect', and the default is 302. I see a lot of 'bad' example code posted on sites that should know better.
3. The [NC] means aNyCase; it will accept both upper and lower case data. The L means 'last rule'; it terminates processing here and immediately issues the redirect, rather than trying to immediately carry out following rules. Always add [L] unless there is a very good reason not to do so.
Note that the test for the domain name isn't end anchored, and therefore will not fix up a www request that includes an appended port number and/or appended period.
Note too, the leading slash on the pattern in the Rule shows that this code is meant for use in httpd.conf, not for use in .htaccess.
# Redirect hostname not exactly "www.example.com" (or blank) to www.example.com
# (also fixes requests with FQDN and requests with appended port numbers)
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]