Home > Software design >  Htaccess RewriteCond Expr not executing
Htaccess RewriteCond Expr not executing

Time:05-20

Per Amazon's Lightsail Troubleshooting I'm trying to update my htaccess to ignore a specific file during the HTTPS redirecting. However, it doesn't seem to be ignoring it.

What I want to happen is, if the user visits http://example.com/health-checker.html they aren't redirected to the HTTPs version. However, if they visit anything else they are redirected to the HTTPs version.

When I use the below code, they are still directed to the HTTPS version. To test if it was a browser caching issue, I used a different file name and still same problem.

RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond expr "! %{REQUEST_URI} -strmatch '*health-checker.html'"
RewriteRule ^(.*)$ https://%{SERVER_NAME}/$1 [R=301,L]

Please note that I'm hard coding my domain name intentionally due to dealing with multiple domain names.

Your assistance is appreciated

CodePudding user response:

You don't really need expr here, you can just use regular RewriteCond with negation like this:

RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{THE_REQUEST} !/health-checker\.html [NC]
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L,NE]
  • Related