Mod_rewrite - Exclude URLs

I need mod_rewrite to redirect all http requests to https , but I want to exclude some urls

 # force https RewriteCond %{HTTPS} off RewriteCond %{HTTP_HOST} ^secure\. [NC] RewriteCond %{REQUEST_URI} !gateway_callback [NC] RewriteRule ^. https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,QSA] 

All URLs matching gateway_callback must be excluded

This URL should not be redirected, but does it do it ??

 http://secure.localhost/da/gateway_callback/29/ 

Tried to reset the DNS cache in the browser, but the url is still redirected to https

+6
source share
2 answers

The main problem with your configuration is that the REQUEST_URI variable contains everything after and after the slash. The third RewriteCond statement should be updated something like this:

 RewriteCond %{REQUEST_URI} !^/da/gateway_callback/.*$ [NC] 

This should be consistent with the example you provided. If the URI does not always start with /da/ , you may need to add a wildcard:

 RewriteCond %{REQUEST_URI} !^/[^/]+/gateway_callback/.*$ [NC] 

where [^/]+ matches one or more characters that are not a slash.

I would recommend always using regex anchors wherever possible, since it eliminates ambiguity. The original RewriteCond trying to match REQUEST_URI does not use them, which may confuse administrators with a casual look.

Also note that all related examples for the RewriteCond and RewriteRule in the official documentation

+7
source

Can a trailing slash keep you from matching? I never understand if the slash is preserved or not. I suggest changing the third condition to

 RewriteCond %{REQUEST_URI} !/gateway_callback/\d+/?$ [NC] 
0
source

All Articles