【问题标题】:htaccess redirect to https only one filehtaccess 仅重定向到 https 一个文件
【发布时间】:2015-04-27 12:55:35
【问题描述】:

我想将所有请求重定向到 index.php 脚本(不是额外的 http 请求,只是内部的)。

当请求 url 是 checkout.html 时,应该会重定向到 https 文件。 所有其他请求都应该是 http。

HTTPs 到 HTTP 可以正常工作,但是 HTTP 到 HTTPS 对 index.php 产生了额外的请求。

这是我的 htaccess 文件:

# Redirect http checkout.html to https
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} checkout\.html [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

# Redirect https to http (excluding checkout.html)
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !checkout\.html [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]

# all script to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^.*\.html|.php$ /index.php [L]

HTTPS 到 HTTP(例如 _http://.../basket.html):

  • 301:将 _https://.../basket.html 重定向到 _http://.../basket.html (正确)
  • 200:http 响应 basket.html

HTTP 到 HTTPS(例如 _http://.../checkout.html)

  • 301:将 _http://.../checkout.html 重定向到 _https://.../checkout.html(正确)
  • 301:将 _https://.../checkout.html 重定向到 _http://.../index.php(不正确)
  • 200:http响应index.php

怎么了??

【问题讨论】:

  • https://...checkout.html 不匹配第一组或第二组条件,因此它被第三组条件拾取。当您从 http://...checkout.html 重定向到安全版本时,该安全页面请求也会通过所有这些 .htaccess 条件。
  • 我不明白为什么当我打开 https://..checkout.html 到 index.php 时会出现 301 重定向(第三个条件),而当我打开 http://...basket.html 时重定向只是索引的内部。 php(也是第三个条件)
  • 第三个条件不是 301 重定向。就是说执行index.php;总会有对该文件的请求。 http://...basket.html 不满足第一组或第二组条件,所以只由第三组处理,即也只是执行 index.php
  • 但是当请求是https://...checkout.html时为什么会有301重定向到index.php?? (例如:websaar.de/checkout.html
  • 如果我请求websaar.de/checkout.html,我会得到一个 302 重定向。这与websaar.de/checkout.html 给出 301 重定向的事实相结合,向我表明真正发生的事情不是您问题中的 .htaccess 中的内容。一个不同的 .htaccess 文件正在执行重定向,或者网络服务器上发生了其他事情。

标签: apache .htaccess redirect


【解决方案1】:

解决办法如下:

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteBase /

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} checkout.html [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !checkout.html [NC]
RewriteCond %{REQUEST_URI} !index.php [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L] 

</IfModule>

我需要将以下行添加到“https on”条件: RewriteCond %{REQUEST_URI} !index.php [NC]

【讨论】:

    猜你喜欢
    • 2016-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-23
    • 1970-01-01
    • 2014-03-16
    相关资源
    最近更新 更多