Forum Moderators: phranque
I'm trying to make a set of rules that redirect if the url ends with index.php without any query string.
So..
http://www.example.com/index.php
..would redirect to http://www.example.com/
http://www.example.com/index.php?var1=2&var2=3
..would not redirect
This is what I have so far, but either I have completely misunderstood how htaccess rules work or I have missed something as it seems to still redirect with a querystring
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php
RewriteCond %{QUERY_STRING} .+
RewriteRule ^index\.php$ http://www.example.com/ [R=301,L]
Can anyone help out with this.. I have tried to follow what has been said in this thread:
[webmasterworld.com...]
..but I'm still having problems
[edited by: jdMorgan at 2:45 pm (utc) on May 28, 2009]
[edit reason] example.com [/edit]
And all of this seems to be the opposite of what you said you want to do in your post: redirect if there is no query string. And that differs from the goal stated in your thread description. It's a bit confusing...
Because it makes little sense to redirect "/index.php" to "/" only if there is no query string, I'll assume that you want to remove the query strings here. One of these examples may suit your needs:
# Redirect to "/" and remove query strings from /index.php requests
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php\?[^\ ]*\ HTTP/
RewriteRule ^index\.php$ http://www.example.co[b]m/?[/b] [R=301,L]
# Redirect to "/" and remove query strings from /index.php or "/" requests
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /(index\.php)?\?[^\ ]*\ HTTP/
RewriteRule ^(index\.php)?$ http://www.example.com/? [R=301,L]
# Redirect to "/" and remove query strings from /index.php or "/" requests
# at any directory level, preserving the requested directory path
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*(index\.php)?\?[^\ ]*\ HTTP/
RewriteRule ^(([^/]+/)*)(index\.php)?$ http://www.example.com/$1? [R=301,L]
Jim
Thanks for the replies.. I'll explain my problem:
basically I have a shopping cart that works off index.php with some extra parameters after it:
www.example.com/index.php?page=shopping_cart
The script that controls the shopping cart doesn't like it if the request comes through without the index.php like this:
www.example.com?page=shopping_cart
..which is what most of the rules I have seen seem to do..
..so basically, i though if I can get it to redirect if there is an index.php WITHOUT a querystring, but leave it as it is if there is a querystring, it would remove the index.php duplicate.
# Rewrite to "/index.php" if query string is present on any request for "/"
# at any directory level, preserving the requested directory path
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*\?[^\ ]*\ HTTP/
RewriteRule ^(([^/]+/)*)$ http://www.example.com/$1index.php [L]