.htaccess 文件中哪个是正确/更好的规则?
我想从我的网站中删除一个已被 Google 索引的 wiki 部分,因此我不想删除它并在删除页面时最终导致大量 404 错误。我想编写一个规则,将 wiki 中的所有 URL 请求页面重定向到另一个页面。所以基本上我想将包含文件夹名称 /wiki/ 的任何 URL 重定向到另一个页面。我有点困惑这应该是 RewriteRule:
RewriteRule ^wiki/* http://www.mydomain.com [R=301,L]
还是 RedirectMatch:
RedirectMatch 301 /wiki/(.*) http://www.mydomain.com
希望得到一些关于哪个是正确/最好的建议以及这是否是正确的方法。
谢谢。
I want to remove a wiki section from my website which has been indexed by Google therefore I don't want to remove it and end up with loads of 404's when the pages are removed. I want to write a rule which will redirect all URL's requesting pages in the wiki to another single page. So basically I want to redirect any URL containing the folder name /wiki/ to another page. I'm a bit confused if this should be a RewriteRule:
RewriteRule ^wiki/* http://www.mydomain.com [R=301,L]
or a RedirectMatch:
RedirectMatch 301 /wiki/(.*) http://www.mydomain.com
Would appreciate some advice on which is correct/best and if this is the correct way to do it.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
他们都会做同样的工作。 但从 SEO 的角度来看,这个想法并不好 - 对于 SEO 来说,返回 410 代码会更好(410 消失了 - 页面曾在此处,但不再可用)。
无论如何:建议使用
RedirectMatch
方法——它使用的 CPU 略少。另一方面,RewriteRule
更加灵活。例如:您的 URL 类似于
example.com/wiki/somepage?say=hello
。如果您使用RedirectMatch
,那么新的URL将为example.com/?say=hello
——正如您所看到的,查询字符串已被复制。使用 RewriteRule,您可以轻松、漂亮地删除它,因此新的 URL 将只是example.com/
。以下是规则(稍作优化):
OR
两个规则中的
?
用于删除现有查询字符串。使用 RewriteRule,当重定向发生时,它就不会出现(mod_rewrite 在这方面非常智能),而 RedirectMatch 非常直接,并且会保留它(如果您正在寻找非常好的/正确的 URL,这可能看起来有点奇怪)They both will do the same job. But this idea is not good from SEO point of view -- for SEO will be better to return 410 code (410 Gone -- page was here but no longer available).
In any case:
RedirectMatch
approach is recommended -- it uses slightly less CPU. On another hand,RewriteRule
is more flexible.For example: you have URL like
example.com/wiki/somepage?say=hello
. If you useRedirectMatch
then new URL will beexample.com/?say=hello
-- as you can see query string was copied over. With RewriteRule you can easily and nicely drop it, so the new URL will be justexample.com/
.Here are the rules (slightly optimized):
OR
The
?
in both rules is for dropping existing query string. With RewriteRule it will not be there when redirect occurs (mod_rewrite is quite intelligent in this regard) while RedirectMatch is very straight forward and will keep it (which may look a bit odd if you are looking for really nice/proper URLs)