I'm trying to make a rewrite rule that redirects only when the variable action is empty to the website index like:
RewriteCond %{REQUEST_URI} /file.php
RewriteCond %{QUERY_STRING} !^action= [NC]
RewriteRule ^([^/]+)/?$ / [L]
Access to file.php should redirect to / and file.php?action=value should not redirect.
But it's not working, what I'm doing wrong?
You can use:
RewriteEngine on
RewriteCond %{QUERY_STRING} !^action= [NC]
RewriteRule ^file\.php / [NC,L,R]
That works if you test the presence of the variable action
.
If you need to test the presence of the empty variable, use:
RewriteCond %{QUERY_STRING} !^action=[^&]+ [NC]
RewriteRule ^file\.php / [QSD,NC,L,R]
[QSD] work only since Apache 2.4.0. If it's not your case, use:
RewriteRule ^file\.php /? [NC,L,R]