guillaumepotier
guillaumepotier

Reputation: 7448

.htaccess force whole site https except some path

I've managed to switch all my website in https through .htaccess with these rules:

RewriteCond %{HTTPS} off [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /app.php [QSA,L]

Now I have a problem, because some 3rd parties services using POST urls like:

http://sub.mydomain.com/agreg/sms/service

Are rejected because they are redirected and now GET.

I'd like to disable this "force https" thing on every url with ^/agreg/ scheme. I tried

RewriteCond %{HTTPS} off [NC]
RewriteCond %{REQUEST_URI} !^/agreg
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,QSA]

But there are now redirected to http://sub.mydomain.com/app.php

What did I miss here?

Thanks a lot for your help!

Upvotes: 0

Views: 961

Answers (1)

Felipe Alameda A
Felipe Alameda A

Reputation: 11799

QUESTION:

I'd like to disable this "force https" thing on every url with ^/agreg/ scheme. I tried

RewriteCond %{HTTPS} off [NC]
RewriteCond %{REQUEST_URI} !^/agreg
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,QSA]

But there are now redirected to http://sub.mydomain.com/app.php


Well, that's a valid result according to the rules.

There is a 2nd rule that redirects everything to app.php. This one:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /app.php [QSA,L]

Although the incoming URL http://sub.mydomain.com/agreg/sms/serviceSo, for example, is excluded from the 1st rule, it is rewritten in the 2nd rule.

SOLUTION:

Assuming the expected behavior is to exclude any URL that holds the string agreg from BOTH rules, it should be something like this:

RewriteCond %{HTTPS} off [NC]
RewriteCond %{REQUEST_URI} !/agreg        [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/agreg        [NC]
RewriteRule ^(.*)$ /app.php [QSA,L]

Rewrite Conditions are valid only for the next rule. It is necessary to add the same NOT condition to the last rule block.

Upvotes: 2

Related Questions