.htaccess force重定向htaccess中重写的url以使用https ssl

时间:2014-02-25 18:10:46

标签: regex .htaccess mod-rewrite ssl https

我想为我的一些重写页面强制使用SSL,以便他们必须使用SSL。

网址为:

http://domain.com/signin
http://domain.com/register

我希望他们成为:

https://domain.com/signin
https://domain.com/register


这是我目前的.htaccess

RewriteEngine on
RewriteRule ^signin$       index.php?&action=showLogin [QSA,L,NC]
RewriteRule ^register$     index.php?&action=showRegister [QSA,L,NC] 


我尝试了以下但是没有成功:

RewriteEngine on
RewriteRule ^signin$       index.php?&action=showLogin [QSA,L,NC]
RewriteRule ^register$     index.php?&action=showRegister [QSA,L,NC] 

RewriteCond     %{SERVER_PORT} !443
RewriteCond     %{REQUEST_URI} ^/(signin|register)
RewriteRule     (.*) https://domain.com/$1 [R]

2 个答案:

答案 0 :(得分:1)

在内部重写之前,您需要重定向到https。

使用此代码:

RewriteEngine on

RewriteCond %{HTTPS} off
RewriteRule ^(signin|register)/?$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC]

RewriteCond %{HTTPS} on
RewriteRule !^(signin|register)/? http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC]

RewriteRule ^signin$       index.php?&action=showLogin [QSA,L,NC]
RewriteRule ^register$     index.php?&action=showRegister [QSA,L,NC]

答案 1 :(得分:0)

http://www.besthostratings.com/articles/force-ssl-htaccess.html

有时您可能需要确保用户通过安全连接浏览您的网站。可以使用包含以下行的.htaccess文件来轻松地将用户重定向到安全连接(https://):

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
Please, note that the .htaccess should be located in the web site main folder.

如果您希望为特定文件夹强制使用HTTPS,则可以使用:

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{REQUEST_URI} somefolder 
RewriteRule ^(.*)$ https://www.domain.com/somefolder/$1 [R,L]
The .htaccess file should be placed in the folder where you need to force HTTPS.