【问题标题】:mod_rewrite: HTTPS for only one pagemod_rewrite:HTTPS 仅用于一页
【发布时间】:2013-11-25 19:53:54
【问题描述】:

我想重定向到仅限 HTTPS 的 /login 页面。其余页面需要是 HTTP。

.htaccess 文件看起来像:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} /login
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !/login
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php

我不知道为什么它不起作用。当我转到 /login 时,我被重定向到 example.com/index.php 怎么了?

【问题讨论】:

    标签: apache .htaccess mod-rewrite https redirect


    【解决方案1】:

    您的目录中是否有一个实际的(物理的)/login 文件夹或文件?我想不是,所以你的/login 请求最终被最后一个重写规则捕获并重定向到 index.php。

    尝试在此行上方添加RewriteCond %{REQUEST_URI} !/loginRewriteCond %{REQUEST_FILENAME} !-f(或者只是以不同的方式处理登录请求)

    【讨论】:

      【解决方案2】:

      尝试在您的最后一条规则中添加L 标志,让您的代码如下:

      RewriteEngine On
      
      RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
      RewriteRule ^ http://example.com%{REQUEST_URI} [R=301,L]
      
      RewriteCond %{HTTPS} off
      RewriteCond %{REQUEST_URI} /login [NC]
      RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
      
      RewriteCond %{HTTPS} on
      RewriteCond %{REQUEST_URI} !/login [NC]
      RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
      
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule . index.php [L]
      

      【讨论】:

        猜你喜欢
        • 2013-03-12
        • 2014-05-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-12
        • 1970-01-01
        相关资源
        最近更新 更多