【发布时间】:2014-03-22 20:52:46
【问题描述】:
我从几个小时以来一直在尝试关注: - .www 请求应重写为非 www - php应该被重写为html - http 应该重写为 https - 所有重定向代码为 301
我正在使用以下 .htaccess 文件:
RewriteEngine on
# www to non www
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com [NC]
RewriteRule (.*) http://mydomain.com/$1 [R=301]
# php to html
RewriteRule ^(.*).html$ $1.php
RewriteCond %{THE_REQUEST} \ /(.+)\.php
RewriteRule ^ /%1.html [R=301]
# http to https
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301]
但它不会工作。如果我删除 http 到 https 规则,我会得到以下结果:
带有 .html 的 www 重定向到末尾带有 .php 的非 www:
:~$ curl -I http://www.mydomain.com/index.html
Location: http://mydomain.com/index.**php**
带有 .php 的 www 重定向到末尾带有 .html 的 www:
:~$ curl -I http://www.mydomain.com/index.php
Location: http://**www.**mydomain.com/index.html
非 www 与 php 作品,它被重定向到 html,这是正确的:
:~$ curl -I http://mydomain.com/index.php
Location: http://mydomain.com/index.html
并且使用 http 到 https 规则一切都搞砸了,因为域被双重插入:
:~$ curl -I http://mydomain.com:81/index.php
Location: https://mydomain.com:81/http://mydomain.com:81/index.html
我了解 http 到 https 重写的问题,因为它已经被重写,所以 url 被双重插入。但是解决这个问题的正确 htaccess 规则是什么?
【问题讨论】:
标签: apache .htaccess mod-rewrite