.htaccess 301使用HTTPS重定向

时间:2014-07-17 15:07:51

标签: php apache .htaccess redirect https

我最近有一个客户端网站更改页面名称,用于搜索引擎优化目的,我们需要重新定向旧链接到新链接,但由于某种原因它无法正常工作。

.htaccess看起来像这样:

AddHandler php5-script .php
RewriteEngine On    # Turn on the rewriting engine
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule    ^([^\.]+)$ $1.php [NC,L]

Redirect 301 /oldpage1  /newpage1
Redirect 301 /oldpage2  /newpage2

但页面被重定向为:

http://examplesite.com:443/newpage1

我甚至试过像这样做301:

Redirect 301 /oldpage1  https://examplesite.com/newpage1

两种方式都会产生相同的结果。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您不应将mod_rewrite规则与mod_alias规则混合使用,并且还需要在内部重写规则之前保留外部重定向规则。

试试这个:

AddHandler php5-script .php
RewriteEngine On

RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

RewriteRule ^oldpage1/?$ /newpage1 [L,NC,R=301]
RewriteRule ^oldpage2/?$ /newpage2 [L,NC,R=301]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
相关问题