.htaccess强制整个站点https除了一些路径

时间:2013-03-17 17:03:10

标签: apache .htaccess https url-rewriting

我已设法通过这些规则通过.htaccess切换https中的所有网站:

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

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

现在我遇到了问题,因为有些第三方服务使用的是POST网址:

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

被拒绝是因为他们被重定向并且现在获得了。

我想在^/agreg/方案的每个网址上禁用此“强制https”功能。我试过了

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

但现在已重定向到http://sub.mydomain.com/app.php

我在这里想念的是什么?

非常感谢你的帮助!

1 个答案:

答案 0 :(得分:2)

问题:

  

我想在每个网址上禁用此“强制https”内容   ^ / agreg / scheme。我试过了

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

但现在已重定向到 http://sub.mydomain.com/app.php


嗯,根据规则,这是一个有效的结果。

第二条规则将所有内容重定向到app.php。这一个:

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

虽然传入的URL http://sub.mydomain.com/agreg/sms/serviceSo例如从第1条规则中排除,但它会在第2条规则中重写。

解决方案:

假设预期的行为是从BOTH规则中排除任何包含字符串agreg的URL,它应该是这样的:

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]

重写条件仅对下一个规则有效。必须将相同的NOT条件添加到最后一个规则块。

相关问题