htaccess文件:除特定页面外,强制转到https而不是http

时间:2016-11-23 17:47:02

标签: php .htaccess http redirect https

我已经阅读了很多关于此的内容,但我仍然无法解决这个问题。我试图强制所有http请求转到https,除了2页。这2页请求具有传递给它们的参数。这是我目前使用的代码:

# 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]

此代码不能完成所需的工作。这有什么问题吗?

1 个答案:

答案 0 :(得分:0)

您应该使用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]

确保在测试此更改之前清除浏览器缓存。

相关问题