htaccess只重定向到https一个文件

时间:2015-04-27 12:55:35

标签: apache .htaccess redirect

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

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

HTTP到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 response basket.html

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

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

出了什么问题?

1 个答案:

答案 0 :(得分: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]