Mod Rewrite Query

Hello,

I’d like to remove every querystring added directly after the domain name in the URL.

For e.g.

http://site.com/?somexyz=xyz
http://site.com/?abc=884&883=444&333=33

So a URL of this type should get redirected to http://site.com/ So basically everything after ? (and including ?) should get removed.

However I do not want the mod-rewrite to affect querystring of any other (inner) pages. For e.g. http://site.com/file.php?abc=xyz or http://site.com/otherfile.php?country=AUS These sort of URLs are okay…

So basically if after the domain name (http://site.com), there is no file name (.html or .php), then the querystring appended directly to domain name should get removed.

I did try the following mod-rewrite rule but it didn’t work:

RewriteRule ^\\?([a-z]+)$ http://site.com/

Please help. Thank you.


# If the root path without any file specification is requested, ...
RewriteCond %{REQUEST_URI} ^$
# ... and the query string is not empty ...
RewriteCond %{QUERY_STRING} !^$
# ... redirect
RewriteRule .? http://example.com/

w.l, WAIT A COTTON PICKIN MINUTE!

You can NOT (read NEVER EVER) match a query string with a RewriteRule statement!

What you need to do is (a) NEVER refer to your DirectoryIndex file as {blank} (this IS what you’re doing with http://www.example.com/?somexyz=xyz) and (b) use a block mod_rewrite statement including a RewriteCond which examines the {QUERY_STRING} (that it exists is sufficient to prevent looping) then test for lack of filename (or the DirectoryIndex filename) in the DocumentRoot before redirecting to the DirectoryIndex file (BY NAME!) but with a trailing ? (that creates a new query string which, because it’s empty, will not even display the ?. Make that a 301 direct, too, so the new {REQUEST_URI} will be displayed.

Remon, your RewriteCond %{REQUEST_URI} ^$ really should be in the RewriteRule as

RewriteRule ^(|index\\.php)$ index.php? [R=301.L]

Regards,

DK

Regards,

DK

So, all combined it would be:


# If the query string is not empty ...
RewriteCond %{QUERY_STRING} !^$
# ... redirect if no file, or index.php (assumed DirectoryIndex) is requested
RewriteRule ^(|index\\.php)$ http://example.com/ [L,R=301]

Am I correct?

Remon,

Almost …

That would make it


# If the query string is not empty ...
RewriteCond %{QUERY_STRING} !^$
# ... redirect if no file, or index.php (assumed DirectoryIndex) is requested
RewriteRule ^(|index\\.php)$ http://example.com/index.php [L,R=301]

Okay, I suppose that calling just the domain is the one exception (WITHOUT the query string) but even that is poor technique (IMHO) IF you are already inside the website (how else would you get a query string attached?). Aw, don’t sweat the petty stuff - that’s just a minor pet peeve of mine.

Regards,

DK