htaccess 301重定向多个文件

时间:2009-08-30 15:16:28

标签: php apache .htaccess mod-rewrite

我想知道htaccess上的多个条目是否适用于301重定向。

我看到的问题是,旧网站文件的扩展名为“html”,名称命名方式不同,因此简单的全局重定向不起作用,每个文件名必须为一条规则。

这个网站每天接收1000次访问,因此需要注意不要惩罚搜索引擎。

RewriteEngine On
RewriteBase /
RewriteRule ^file1\.html$ http://www.domain.com/file1.php [R=301,NC,L]
RewriteRule ^file2\.html$ http://www.domain.com/file2.php [R=301,NC,L]
RewriteRule ^file3\.html$ http://www.domain.com/file3.php [R=301,NC,L]
RewriteRule ^file4\.html$ http://www.domain.com/file4.php [R=301,NC,L]

由于旧文件是html类型,因此php标头重写不起作用。

3 个答案:

答案 0 :(得分:3)

我想你可以使用一些正则表达式来减少你正在使用的不同RewriteRules的数量,因为它们看起来都是一样的。

在你的情况下,只使用这个可能没问题:

RewriteRule ^(file1|file2|file3|file4)\.html$ http://www.metaboforte.com/$1.php [R=301,NC,L]

这样,您可以准确指定要重写的内容;但只有1个RewriteRule。

或者,更通用一点:

RewriteRule ^file([0-9]*)\.html$ http://www.metaboforte.com/file$1.php [R=301,NC,L]

允许您定义要重写每个fileXYZ.html,XYZ是一个数字。 (因为我使用'*',重写规则不会考虑任何数字;如果你想要至少一个数字,你应该使用'+')

你也可以做一些更通用的事情 - 不确定你想要那样,但是这样的事情可能会这样做:

RewriteRule ^(.*?)\.html$ http://www.metaboforte.com/$1.php [R=301,NC,L]

在这里,您将重定向以.html结尾的所有内容

答案 1 :(得分:1)

为什么不呢......

RewriteRule ^file([0-9]+)\.html$ http://www.metaboforte.com/file$1.php [R=301,NC,L]

或者,如果你想在旧网站上重写一切 ......

RewriteRule ^(.*?)\.html$ http://www.metaboforte.com/$1.php [R=301,NC,L]

答案 2 :(得分:0)

也许你可以试试RewriteMap Directive