Forum Moderators: phranque

Message Too Old, No Replies

301 Redirect issue on browsers

Firefox okay but not Chrome, Safari & IE

         

mightyasthore

3:05 am on Apr 17, 2012 (gmt 0)

10+ Year Member



I have this issue with URL redirection. From w/out www to w/ www. I managed to fix the redirection on Mozilla Firefox but it doesn't seem to work on Google Chrome, IE & Safari. Can anyone help me?

.htacess codes are:

Options -Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} ^rentaloffice\.sg$ [OR]
RewriteCond %{HTTP_HOST} ^www\.rentaloffice\.sg$
RewriteCond %{HTTP_HOST} ^rentaloffice\.sg$ [OR]
RewriteCond %{HTTP_HOST} ^www\.rentaloffice\.sg$
RewriteRule ^index\.php$ "http\:\/www\/.rentaloffice\.sg\/" [R=301,L]


Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [L,QSA]

lucy24

7:04 am on Apr 17, 2012 (gmt 0)

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



The redirect doesn't happen in the browser. It happens in your server. Now, by weird coincidence it is less than 48 hours since someone else in this Forum discovered that different browsers have different ways of handling an explicitly typed "index.html" or similar. That may be why they seem to handle the redirect differently. But by itself it is not the cause of any problems.

The conditions as written are potentially calamitous, with those pairs of [OR] mixed in with the default AND.

You need only one Condition, identifying any requested host that is NOT in the form you want:

RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

:: fingers crossed ::

This comes after all page-specific redirects. Those will include the http://www.example.com as part of the redirection, so you don't need to check the host at all.

No escaping in the target. Slashes do not require escaping in mod_rewrite.

Options should be collected into a single line at the top of your htaccess. The RewriteEngine only needs to be turned on once per htaccess.

mightyasthore

9:40 am on Apr 17, 2012 (gmt 0)

10+ Year Member



I tried using only one condition and it worked well for different browsers but the problem is, the subpages shows 404 Not Found errors. But if I use the above .htaccess structure, it only fix 301 redirect for Firefox and not on other browsers but all the subpages are working. Can you at least give me some ideas why this thing happens?

g1smd

10:07 am on Apr 17, 2012 (gmt 0)

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



RewriteRule ^index\.php$ http://www.example.com/ [R=301,L]

(note corrected syntax) will redirect requests for www.example.com/index.php to www.example.com/

Your
DirectoryIndex
directive will map this new request to the internal file at index.php

index.php matches the pattern in your
RewriteRule
and the request will be externally redirected to www.example.com/

Your
DirectoryIndex
directive will map this new request to the internal file at index.php

index.php matches the pattern in your
RewriteRule
and the request will be externally redirected to www.example.com/

... and so on until the browser or server gives up.

You need a preceding
RewriteCond
looking at
THE_REQUEST
server variable. This ensures that the redirect happens only when index.php has been externally requested as a URL and not as a result of a prior internal rewrite or
DirectoryIndex
mapping.

The code you need appears in at least 5000 of the previous threads in this forum. It's a regular question here. :)