更改某些文件名后如何执行301永久重定向

时间:2018-08-02 16:37:39

标签: .htaccess url-redirection http-status-code-301

我们有一个非常老的网站正在更新。旧站点中的所有文件都具有.htm扩展名,我们希望将其更改为.html扩展名。有些文件名将保持不变(扩展名除外),而另一些文件名我们希望更改。当时的想法是使用RedirectMatch 301将所有htm页面重定向到html扩展名,然后仅对具有新文件名的页面列出Redirect 301。

RedirectMatch 301 (.*).htm$ http://www.example.com$1.html
Redirect 301 /old-page-1.htm http://www.example.com/new-page-1.html
Redirect 301 /old-page-2.htm http://www.example.com/new-page-2.html

是完成此任务的最佳方法还是更好的方法?

1 个答案:

答案 0 :(得分:0)

首先要有特定的规则,然后要有通用的规则。同样,最好对所有这样的规则使用RedirectMatch

RedirectMatch 301 /old-page-1\.htm$ http://www.example.com/new-page-1.html
RedirectMatch 301 /old-page-2\.htm$ http://www.example.com/new-page-2.html
RedirectMatch 301 ^(.+)\.htm$ http://www.example.com$1.html

请确保在新的浏览器中对此进行测试,以避免旧的缓存。