Mod重写和重定向不起作用



有重定向问题。我想把我所有的url从php重写到html,所以我添加了这个规则:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?).html$ $1.php [NC,L]

工作amazing,我可以查看页面时,我转到示例:/index.html但这对SEO不好,因为当我可以从index.php访问页面时。。。如何进行良好的重定向?当我尝试使用以下代码重定向时:

RewriteCond %{REQUEST_URI} ^/index.php$ [NC] 
RewriteRule ^index.html  /index.php [R=301,L,NC]

不重定向不起作用。。。我在哪里犯了错误?

您需要一个额外的重写器来将/index.html内部重定向到/index.phptry:

##1)redirect /index.php to /index.html##
RewriteCond %{THE_REQUEST} /index.php [L,R,NC]
RewriteRule ^ /index.html [NC,L,R]
##2)internally redirect /index.html to /index.php##
RewriteRule ^index.html$ /index.php [L,NC]

不要重定向,而是像这样将php作为html:

# Process PHP within .html  
AddType application/x-httpd-php .php .htm .html

(您必须将file.php重命名为file.html)

最新更新