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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With