htaccess文件:强制去https而不是http,除了特定的页面

问题描述:

我已经阅读了很多关于这个,但我仍然无法弄清楚我的情况。我试图强制所有http请求转到https,除了2页。这两个页面请求传递给它们的参数。以下是我目前使用的代码:htaccess文件:强制去https而不是http,除了特定的页面

# Force go to https if user isn't asking for a url that has 'register_user.php' or 'register-customer' inside the url: 
    RewriteCond %{SERVER_PORT} 80 
    RewriteCond %{REQUEST_URI} !^/register_user.php$ [OR] 
    RewriteCond %{REQUEST_URI} !^/register-customer$ [NC] 
    RewriteRule ^(.*)$ https://www.myWebsite.com/main-folder/$1 [R,L] 

# Force go to http if user is asking for a url that has 'register_user.php' or 'register-customer' inside the url: 
    RewriteCond %{SERVER_PORT} !80 
    RewriteCond %{REQUEST_URI} ^/register_user.php$ [OR] 
    RewriteCond %{REQUEST_URI} !^/register-customer$ [NC] 
    RewriteRule ^(.*)$ http://www.myWebsite.com/main-folder/$1 [R,L] 

此代码不执行所需的工作。它有什么问题吗?

+0

可能重复的[如何将所有HTTP请求重定向到HTTPS](http://stackoverflow.com/questions/4083221/how-to-redirect-all-http-requests-to-https) –

+0

@Jeremy ..不完全是! ...我想从此重定向中排除特定页面。 – Brad

您应该使用THE_REQUEST而不是REQUEST_URI,因为REQUEST_URI可能因其他规则而改变。

你也应该保持在上面这条规则之前的其他规则:

# Force go to https if user isn't asking for a url that has 'register_user.php' or 'register-customer' inside the url: 
RewriteCond %{HTTPS} off 
RewriteCond %{THE_REQUEST} !/(register-customer|register_user\.php)[?/\s] [NC] 
RewriteRule^https://%{HTTP_HOST}/main-folder%{REQUEST_URI} [R=301,NE,L] 

# Force go to http if user is asking for a url that has 'register_user.php' or 'register-customer' inside the url: 
RewriteCond %{HTTPS} on 
RewriteCond %{THE_REQUEST} /(register-customer|register_user\.php)[?/\s] [NC] 
RewriteRule^http://%{HTTP_HOST}/main-folder%{REQUEST_URI} [R=301,NE,L] 

确保测试这种变化之前,清除浏览器缓存。

+1

非常感谢:) ......完美的作品。 – Brad