Forum Moderators: phranque
For example http://www.example.com/index.html?wrong will redirect twice no matter which order I put the redirects in.
Can anyone help.
Also, if there is any other issues that you can see I would be grateful if you would mention it.
## REMOVE SPURIOUS QUERY STRINGS
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /[^?]+\? [OR]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?
RewriteRule (.*) http://www.example.com/$1? [R=301,L]
#
## REDIRECT /INDEX.HTML OR DIR/INDEX.HTML TO / OR /DIR/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html$ http://www.example.com/$1 [R=301,L]
#
## ADD SLASH IF DIRECTORY URI DOESN'T CONTAIN A TRAILING SLASH
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\..+$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule (.*) http://www.example.com/$1/ [R=301,L]
#
## REMOVE SLASHES IF URI CONTAINS MULTIPLE SLASHES
RewriteCond %{REQUEST_URI} //
RewriteRule ([^/]*)/+(.*) http://www.example.com/$1/$2 [R=301,L]
#
## REMOVE SLASHES IF URI CONTAINS MULTIPLE SLASHES BEFORE URI-PATH
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ //+([^\ ]*)
RewriteRule (.*) http://www.example.com/%1 [R=301,L]
#
## REDIRECT NON-WWW TO WWW
RewriteCond %{HTTP_HOST} !^w{3}\.example.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
## REDIRECT HTTPS TO HTTP
RewriteCond %{HTTPS} =on
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
Thanks
Ray...
Jim
That way, your existing 'parameter' rule would then only need to run for non-index URLs. Likewise, your index 'redirect' would run for index URLs without parameters.
There's at least one unescaped period in your code example above. That's something that I often miss, too.
.
[edit]Actually, no. Looking again. Just moving your index rule to be first, and adding a question mark to the target URL would mean it would strip parameters if present and still operate to strip the index part even if they weren't present. No need for an extra rule at all. Just need the "most specific rule first"; in this case "index" is more specific than "URL with parameters".
I would also put your "slashes" rules before the 'parameters' rule, and have those 'slashes' rules also strip out parameters if they happened to be present. Just add a question mark to the target URL in your 'slashes' rules.
All of your rules force www if the request was for non-www while doing whatever the specific rule is also meant to be doing. Make it so each one also strips parameters at the same time too.[/edit]