.htaccess: check if the query string has a specific value or redirects it

I am trying to find out a little .htaccess and am really worried about what it can do. I saw a fragment online, but I can not get it to work, basically it looks like . If the query string does not have a specific value, redirect it to index.php or to another page. How to do it?

The apple values ​​are displayed here:

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

Instead, it will be redirected to index.php:

 www.domain.com/somefile.php?a=stinkycheese&b=xyz 
+4
source share
1 answer

You need to use the% {QUERY_STRING} variable inside the RewriteCond. For example, if you do not want to redirect, if there is a redirect=no line in the query line, it will look like this:

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

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

+15
source

Source: https://habr.com/ru/post/1381366/


All Articles