【问题标题】:.htaccess force redirect a rewritten url in htaccess to use https ssl.htaccess 强制重定向 htaccess 中重写的 url 以使用 https ssl
【发布时间】:2014-02-27 00:15:11
【问题描述】:

我想对我的一些重写页面强制使用 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]

【问题讨论】:

    标签: regex .htaccess mod-rewrite ssl https


    【解决方案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]
    

    【讨论】:

    • 我也在尝试做相反的规则,但它不起作用? RewriteCond %{HTTPS} on RewriteRule ^!(signin|signup)/?$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC]
    【解决方案2】:

    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.
    

    【讨论】:

    • 我需要重定向单个文件(signin|register)而不是整个文件夹站点?我已经用谷歌搜索了那个网站,但它无法提供我需要的东西。
    猜你喜欢
    • 2012-07-30
    • 2011-01-27
    • 1970-01-01
    • 2016-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多