【问题标题】:To remove .php AND send to https from http connection删除 .php 并从 http 连接发送到 https
【发布时间】:2015-11-11 14:58:45
【问题描述】:

我们将整个站点从 http:// 移至 https://,如果您通过 https:// 到达该站点,这将正常工作。但是,http:// 上所有以前缓存的 url(在 Google 中)都需要重定向。此外,我们之前一直在使用重写来删除 url 中的 .php 并希望保留它。但这是我们遇到问题的地方。

我们在整个站点中从 http 到 https 页面的重定向与下面的 htaccess 一起使用,但它不会删除这些重定向上的 .php 扩展名。

试过这些 Force HTTPS on certain URLs and force HTTP for all others

问题来了

这是一个旧网址示例,看看它是如何在 .php 未删除的情况下重写的。 http://www.myitalianliving.com/product/1105/translucent-stackable-indoor-outdoor-chair-cooler-by-scab

这里是网站 https://www.myitalianliving.com/

这是 .htaccess 文件

RewriteEngine on

## provide ip address redirect to canonical and SEO puproses
## see: https://stackoverflow.com/questions/9985735/redirect-ip-to-domain
#
#RewriteCond %{HTTP_HOST} ^81\.29\.78\.50$ [NC,OR]
#RewriteCond %{HTTP_HOST} ^([a-z.]+)?myitalianliving\.com$ [NC]
#RewriteCond %{HTTP_HOST} !^www\. [NC]
#RewriteCond %{SERVER_PORT} 80
#RewriteRule ^(.*)$ https://www.myitalianliving.com/$1 [R=301]
#
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/ 
RewriteRule ^index\.php$ https://www.myitalianliving.com/ [R=301] 
RewriteRule ^index\.html$ https://www.myitalianliving.com/ [R=301] 
RewriteRule ^index\.htm$ https://www.myitalianliving.com/ [R=301]
## added below as in testing we had just the .com/index in the url no      extension.
RewriteRule ^index https://www.myitalianliving.com/ [R=301]
#

##########################
# Try to remove .php AND send to https from http connection
##########################
Options +SymlinksIfOwnerMatch +MultiViews
#
#### RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
##### RewriteRule ^(.*).php/(.*) $1.php?$2
RewriteRule ^(.*?).php/(.*?) $1.php?$2 [NC]
#
## https://stackoverflow.com/questions/4398951/force-ssl-https-using- htaccess-and-mod-rewrite
#
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

【问题讨论】:

标签: .htaccess http redirect https


【解决方案1】:

您需要将 HTTPS 重定向移至规则集的顶部:

RewriteEngine on

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

## provide ip address redirect to canonical and SEO puproses
## see: http://stackoverflow.com/questions/9985735/redirect-ip-to-domain
#
#RewriteCond %{HTTP_HOST} ^81\.29\.78\.50$ [NC,OR]
#RewriteCond %{HTTP_HOST} ^([a-z.]+)?myitalianliving\.com$ [NC]
#RewriteCond %{HTTP_HOST} !^www\. [NC]
#RewriteCond %{SERVER_PORT} 80
#RewriteRule ^(.*)$ https://www.myitalianliving.com/$1 [R=301]
#
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/ 
RewriteRule ^index\.php$ https://www.myitalianliving.com/ [L,R=301] 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html?\ HTTP/ 
RewriteRule ^index\.html?$ https://www.myitalianliving.com/ [L,R=301] 
## added below as in testing we had just the .com/index in the url no      extension.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\ HTTP/ RewriteRule ^index https://www.myitalianliving.com/ [L,R=301]
#

##########################
# Try to remove .php AND send to https from http connection
##########################
Options +SymlinksIfOwnerMatch +MultiViews
#
#### RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
##### RewriteRule ^(.*).php/(.*) $1.php?$2
RewriteRule ^(.*?).php/(.*?) $1.php?$2 [L,NC]

此外,您需要在索引重定向规则中添加条件并使用L 标志(以防万一)。

【讨论】:

  • 已经这样做了,但 .php 仍然没有被删除。
【解决方案2】:

将 HTTPS 重定向移动到顶部与底部的以下重定向一起使用。

# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

# Rewrite URLs for processing by router (article.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^article/(.+)$ article.php?url=$1 [QSA,L]

# Rewrite URLs for processing by router (product.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^product/(.+)$ product.php?url=$1 [QSA,L]

# Rewrite URLs for processing by router (page.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page/(.+)$ page.php?url=$1 [QSA,L]

# Rewrite URLs for processing by router (section.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ section.php?url=$1 [QSA,L]

【讨论】:

    猜你喜欢
    • 2014-01-19
    • 1970-01-01
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    • 2012-01-09
    • 2020-09-06
    • 1970-01-01
    • 2011-06-19
    相关资源
    最近更新 更多