Redirecting Query String link with .htaccess

Hey guys,

This seems like a simple issue to resolve, but what I’m doing doesn’t seem to work. I want to redirect a URL with a specific querystring to the root. For example:

The url:

http://mywebsite.com/?view=home

should redirect to:

http://mywebsite.com/

The code I’m using in my .htaccess file is:

RewriteEngine on
RewriteCond %{QUERY_STRING} ^view=home$
RewriteRule ^/$ http://mywebsite.com/? [L,R=301]

But it’s not working. I’m not getting a server error, it’s just not recognizing the redirect, thus it stays at “?view=home”.

What makes this situation unique is that there is no “page” present in the URL, because I’m not displaying the “index.php”, but this doesn’t work either:

RewriteEngine on
RewriteCond %{QUERY_STRING} ^view=home$
RewriteRule ^/index.php$ http://mywebsite.com/? [L,R=301]

Anyone have any idea how to properly redirect a URL with a querystring on the root (with no page in the url)?

Thanks,
Louis

Louis,

Yes, it is rather simple. Your code, however, is requiring that the {REQUEST_URI} string be empty AND you are killing any previous query string in the redirection (which is proper). All you need to do is change the ^/$ regex (which, BTW, also requires Apache 1.x) to .? and your code will be fine (to send ANY/ALL URIs with the 'view=home" query string - ONLY - to the home page).

RewriteEngine on
RewriteCond %{QUERY_STRING} ^view=home$
RewriteRule .? http://mywebsite.com/? [L,R=301]

Regards,

DK

David, thanks very much, that worked like a charm. Now I gotta get myself a good book on RegExp and/or .htaccess. :slight_smile:

Louis