Mod重写和重定向不起作用

时间:2016-04-09 15:03:13

标签: apache .htaccess redirect mod-rewrite

有重定向问题。我想从php重写扩展到html所有我的网址,所以我添加这个规则:

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

作品效果惊人,我可以在以下示例中查看页面:/index.html 但它对SEO不好,因为当我也可以从index.php访问页面... 如何做好重定向?当我尝试使用此代码重定向时:

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

它没有工作没有重定向......我犯了哪个错误?

2 个答案:

答案 0 :(得分:0)

您需要额外的重写内容将 /index.html 内部重定向到 /index.php 尝试:

##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]

答案 1 :(得分:0)

不要进行重定向,而是将php作为html服务:

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

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