【问题标题】:htaccess rewrite rule fails on https redirectionhtaccess 重写规则在 https 重定向上失败
【发布时间】:2014-01-16 08:54:16
【问题描述】:

我的 htaccess 是这样的

RewriteEngine On
RewriteCond %{HTTP_HOST} !^localhost$
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteRule ^login/?(.*)$ login.php$1 [L]
RewriteRule ^register/?(.*)$ register.php$1 [L]

我想重定向我的页面登录并重定向到 https,所以我添加了下面的代码

RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} !^/(login|register)/?$
RewriteRule .*$ http://%{HTTP_HOST}%{REQUEST_URI} [L]

所以当我输入 http://www.xyz.com/login/ 时,它会重定向到 https://www.xyz.com/login/ 一切正常很好,当我决定将页面重定向到未登录或注册的 http 时,我写了

RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} !^/(login|register)/?$
RewriteRule .*$ http://%{HTTP_HOST}%{REQUEST_URI} [L]

之后我再次测试了网址 http://www.xyz.com/login/ 并将我重定向到 https://www.xyz.com /login.php

http://www.xyz.com/login/ 重定向到 https://www.xyz.com/login/ 并在该页面上的任何想法当我点击链接 https://www.xyz.com/news/1/ 重定向到 http://www.xyz.com/news/1/

【问题讨论】:

    标签: apache .htaccess mod-rewrite redirect


    【解决方案1】:

    尝试将其添加到RewriteEngine On 行的正下方:

    RewriteCond %{HTTPS} off
    RewriteRule ^(login|register)(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]
    
    RewriteCond %{HTTPS} on
    RewriteRule !^(login|register) http://%{HTTP_HOST}%{REQUEST_URI} [L,R]
    

    您的规则似乎也粘贴错了,这两个规则块是相同的。

    【讨论】:

    • 感谢您的回答,是的,第三个代码块是错误的。我试过你的代码,不幸的是它把我重定向到 http://www.xyz.com/**login.php** 而不是 http://www.xyz。 com/login/
    • @ilazmi 您在将login 重写为login.php 的两条规则之前有它吗?它必须位于规则的顶部
    猜你喜欢
    • 1970-01-01
    • 2020-11-01
    • 2016-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多