Forum Moderators: phranque

Message Too Old, No Replies

Redirect index.php without query string

How do I redirect if index.php has a querystring?

         

Noodlewitt

2:19 pm on May 28, 2009 (gmt 0)

10+ Year Member



Hi all,

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]

jdMorgan

2:44 pm on May 28, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your first RewriteCond matches any request for "index.php" -- with or without a query string
Your second RewriteCond requires that the query string be present
Your RewriteRule does not clear or replace the query string.

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]

-or-

# 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]

-or-

# 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]

Note that in all examples, the redirect is invoked if any query string --including a blank one-- is appended to the requested /index.php URL-path. So they will all redirect requested URLs like "/index.php?" or "/index.php?foo=bar" to "/".

Jim

g1smd

7:11 pm on May 28, 2009 (gmt 0)

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



Without the redirect, URLs with index.php and without index.php (for the same query string) in them are duplicates.

I would always omit index.php from the URL and redirect if it is requested - whether or not there are any parameters present.

Noodlewitt

8:17 am on May 29, 2009 (gmt 0)

10+ Year Member



Hi there,

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.

g1smd

9:01 am on May 29, 2009 (gmt 0)

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



I would redirect all URL requests (for index.php and/or non-www) to omit the index.php part and force the www and then I would set up a either the DirectoryIndex parameter or a rewrite to call the index.php filename internally from that canonical URL request.

jdMorgan

1:43 pm on May 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, this calls for a rewrite, not a redirect.

# 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]

Jim