扩展名为.php的网页重定向到.html

时间:2013-09-23 08:49:29

标签: php .htaccess redirect mod-rewrite

我正在尝试将扩展名为.php的所有网页重定向到.html 使用以下代码:

Options +FollowSymLinks

RewriteEngine On

RewriteRule ^(.*)\.php$ /rules/$1.html [R=301,L]

RewriteRule ^([^\.]+)\.html$ /rules/$1.php [NC,L]

但收到错误“此网页有重定向循环”

2 个答案:

答案 0 :(得分:0)

你已经完成了.php到.html的第一次重定向,之后你将.html改为.php重定向,这样就会重定向递归。

制作这样的代码。这会将.php重定向到.html文件。

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)\.php$ /rules/$1.html [R=301,L]

答案 1 :(得分:0)

用以下代码替换您的代码:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# To externally redirect /dir/foo.php to /dir/foo.html
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php[\s?] [NC]
RewriteRule ^ %1.html? [R=301,L]

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