Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess: Check if query string has a certain value or else redirect it

I'm trying to learn a bit of .htaccess and am really anxious on what it can do. I saw a snippet online but can't get it to work, it basically goes like this If a query string does not have a certain value, redirect it to index.php instead or some other page. How do I do that?

This looks for the value apples:

www.domain.com/somefile.php?a=apples&b=xyz

This will redirect to index.php instead:

www.domain.com/somefile.php?a=stinkycheese&b=xyz
like image 211
enchance Avatar asked Nov 15 '11 19:11

enchance


1 Answers

You need to use the %{QUERY_STRING} variable inside a RewriteCond. So for example, if you don't want to redirect if there exists a redirect=no in the query string, it would look like this:

RewriteCond %{QUERY_STRING} !(^|&)redirect=no($|&)
RewriteRule . /index.php [L]

So if the RewriteCond fails (it has a redirect=no in the query string), then do not apply the following rule, which rewrites whatever the request URI is to /index.php. The actual rule that you need may be different, but using RewriteCond and %{QUERY_STRING} is the basic idea that you want.

like image 111
Jon Lin Avatar answered Dec 18 '22 04:12

Jon Lin