I’m having trouble getting non-matching (negation) mod_rewrite patterns to work.
In the example below I want all requests redirected to “/promo-page”, however, don’t allow a redirect loop (hence the RewriteCond).
### Request URL: http://example.com/promo-page ###
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_URI} !/promo-page
RewriteRule .* /promo-page [R=302,L]
When a request for “/promo-page” is made, the RewriteCond rule is ignored and a redirect loop occurs.
Any ideas why the Exclamation Mark (!) isn’t working? Is the syntax incorrect?
Thanks in advance.
UPDATE
I’ve also tried these rules in an online mod_rewrite tester and they also don’t work (if you use “home” as the URL and “/home” as the REQUEST_URI).
Thanks all for your answers and thoughts. I finally tracked down the issue, so here’s the details in the hope it may help someone else down the track.
What was confusing the situation was more rules further down in the .htaccess file. These weren’t posted in my original question as I didn’t believe they were relevant and would even affect the outcome.
These additional rules re-write the Clean URL’s into a GET request. I was under the (false) impression that %{REQUEST_URI} also includes the GET variables tacked onto the end of URL’s. It doesn’t. You must use %{QUERY_STRING} to separately check the content of the GET query string.
Mod-Rewrite Logging was very useful in helping me debug this issue. I wasn’t able to enable it on our live/production cPanel server, so I installed XAMPP on my PC, enabled logging and played with the rewrite rules locally until I got it working.