http重定向到https,不能100%工作

时间:2017-03-20 20:05:32

标签: .htaccess redirect mod-rewrite

下面的代码工作正常,除了没有http网址被重定向到完整的网址到https ...他们被一个301扔到root。甚至带有section.php的http网址也被正确地重定向到完整的https网址。

请参阅此处,http重定向无法正常工作。 http://www.jamavarrestaurants.com/s/6/mayfair-private-dining-with-private-room

在这里工作,但我们不想在网址中使用section.php。 http://www.jamavarrestaurants.com/section.php/6/mayfair-private-dining-with-private-room

注意:只是为了混淆我们复制了包含SEF检查和URL结构的section.php文件的问题并制作了副本" s.php"现在或应该使用它。对于products.php,article.php和discount.php

也是如此

非常感谢任何帮助。

#Header append X-FRAME-OPTIONS "SAMEORIGIN"
RewriteEngine On 
RewriteBase /

## HTTP > HTTPS (only works for this format 
http://www.jamavarrestaurants.com/section.php/3/1/mayfair-restaurant )
## see: http://www.codexworld.com/redirect-non-www-to-www-http-to-https-
using-htaccess-file/
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.jamavarrestaurants.com%{REQUEST_URI} [NE,L]

## RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/ 
# RewriteRule ^index\.php$ https://www.jamavarrestaurants.com/ [R=301,L] 
#
## Added beciause the catch all 404 redirect was sending to /index
# RewriteRule ^index?$ https://www.jamavarrestaurants.com [L,R=301,NC]
#
### JSHOP CORE REWRITE RULES ###
################################
#<IfModule mod_rewrite.c>

Options +SymlinksIfOwnerMatch +MultiViews
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?).php/(.*?) $1.php?$2
## special rewrite rules for shortened urls ##
RewriteRule ^section.php/(.*) /s/$1 [R=301,L]
RewriteRule ^section/(.*) /s/$1 [R=301,L]
#
### To remove section page if 1 which is default for most (unless products 
span more)
RewriteRule ^s.php/(.*)/1/(.*) /s/$1/$2 [R=301,L]

#RewriteRule ^s/(.*)/1/(.*) /s/$1/$2 [R=301,L]
#
RewriteRule ^product.php/(.*) /p/$1 [R=301,L]
RewriteRule ^product/(.*) /p/$1 [R=301,L]
RewriteRule ^article.php/(.*) /a/$1 [R=301,L]
RewriteRule ^article/(.*) /a/$1 [R=301,L]
RewriteRule ^discount.php/(.*) /d/$1 [R=301,L]
RewriteRule ^discount/(.*) /d/$1 [R=301,L]

#</IfModule>
###########################
# The rest
############################

1 个答案:

答案 0 :(得分:1)

重构并重新排序您的规则。用这个替换.htaccess:

Options +SymlinksIfOwnerMatch +MultiViews
RewriteEngine On 
RewriteBase /

## HTTP > HTTPS (only works for this format 
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(\S*)\sHTTP [NC]
RewriteRule ^ https://www.jamavarrestaurants.com/%1 [NE,L,R=301]

## special rewrite rules for shortened urls ##
RewriteRule ^section(?:\.php)?/(.*)$ /s/$1 [R=301,L]

### To remove section page if 1 which is default for most (unless produccts span more)
RewriteRule ^s\.php/(.*)/1/(.*) /s/$1/$2 [R=301,L]

RewriteRule ^product(?:\.php)?/(.*)$ /p/$1 [R=301,L]

RewriteRule ^article(?:\.php)?/(.*)$ /a/$1 [R=301,L]

RewriteRule ^discount(?:\.php)/(.*)$ /d/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)\.php/(.*)$ $1.php?$2 [L,QSA]

在测试此更改之前,请务必完全清除浏览器缓存。

curl测试输出:

curl -kI -A "Chrome" 'http://www.jamavarrestaurants.com/s/5/mayfair-food-and-wine-menus'
HTTP/1.1 301 Moved Permanently
Date: Mon, 20 Mar 2017 21:40:31 GMT
Server: Apache
Location: https://www.jamavarrestaurants.com/s.php/5/mayfair-food-and-wine-menus
Content-Type: text/html; charset=iso-8859-1