我需要我的.htaccess文件将所有<any_name> .php重定向到<same_name> .html除了index.php </same_name> </any_name>

时间:2012-10-20 06:10:55

标签: .htaccess mod-rewrite redirect rewrite

我需要一个.htaccess重写规则,它会重定向这样的链接:

  • www.mydomain.com/viewpressrelease.php
  • www.mydomain.com/viewpressrelease.php?id=17(或以下任何文字.php)
  • www.mydomain.com/employment.php

使用相同的名称将.php替换为.html,如下所示:

  • www.mydomain.com/viewpressrelease.html
  • www.mydomain.com/employment.html

但不对index.php或以下内容采取行动:

  • www.mydomain.com/administrator/index.php

我有一个关闭但不处理的示例?id = 17 part并将index.php转换为index.html。

RewriteCond %{THE_REQUEST} ^[A-Z]+\s([^\s]+)\.php\s 
RewriteRule .* %1.html [R=301,L] 
RewriteRule ^(.*)\.html$ $1.php

根据需要,我需要做什么工作?

1 个答案:

答案 0 :(得分:1)

您需要添加条件以排除index.php,然后从针对\s的检查中移除最后%{THE_REQUEST}

RewriteCond %{REQUEST_URI} !index\.php
RewriteCond %{THE_REQUEST} ^[A-Z]+\s([^\s]+)\.php
RewriteRule .* %1.html [R=301,L] 
RewriteRule ^(.*)\.html$ $1.php