2017-09-15 14 views
-3

我已经搜索了这个问题,但没有答案可行我。如何使用.htaccess强制重定向到除一个文件夹之外的HTTPS?

我想重定向我的整个网站从HTTP到HTTPS除了文件夹/游戏/中的项目。

我想对/ games /做相反的处理,从HTTPS重定向到HTTP。

这是我试过但它只是显示一个无限循环。

RewriteEngine On 
RewriteCond %{HTTPS} off 
RewriteCond %{REQUEST_URI} !games [NC] 
RewriteRule^https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 
+0

我试过[this](https://stackoverflow.com/questions/17638645/force-https-except-for-one-directory),但它不工作 - > RewriteEngine在 RewriteCond%{HTTPS} off RewriteCond%{REQUEST_URI}!游戏[NC] RewriteRule^https://%{HTTP_HOST}%{REQUEST_URI} [L,R = 301] – leonylyner

回答

0

您可以使用此规则为第一条规则在网站根目录的.htaccess:

RewriteEngine On 

RewriteCond %{HTTPS} !on 
RewriteCond %{THE_REQUEST} !games [NC] 
RewriteRule^https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE] 

# rest of your rules go here 

确保测试这些修改时清除浏览器缓存。

+0

谢谢。但是这只适用于主页和/ games /中的页面。对于其他目录中的其他页面(例如http://example.com/file/index.html),它将变成无限循环。 – leonylyner

+0

您误解了它或没有正确解释您的问题。如果请求中有“游戏”,则此规则**跳过**。为什么一旦请求在重定向后已经成为'https',它就会变成无限循环。 – anubhava

+0

这条规则必须是您在'RewriteEngine'行下的第一条规则。 – anubhava

相关问题