Home > database >  How to use RewriteCond in .htaccess in negative
How to use RewriteCond in .htaccess in negative

Time:10-15

the rule I showed works perfectly for all urls that include the "checked" parameter.

But what I need is for the rule to run if the url does NOT contain the "checked" parameter. That is, the same rule in negative.

I tried to use "!" Just before I "checked" but it didn't work.

<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} checked [NC]
RewriteRule (.*) /myscript.php?url=https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,QSD]
</IfModule>

CodePudding user response:

You could try either of these rules in .htaccess rules file. Make sure to use one at a time only, depending upon your usage.

Make sure to clear your browser's cache before testing your URLs.

1st rules set: In case you want to match(negate) exact word checked then use this rule set.

<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} !\bchecked\b [NC]
RewriteRule (.*) /myscript.php?url=https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,QSD]
</IfModule>

OR

2nd rules set: In case you can't have anything apart from checked in your rules set then you can simply negate it.

<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} !checked [NC]
RewriteRule (.*) /myscript.php?url=https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,QSD]
</IfModule>
  • Related