重新设计网站后,我有几页需要重定向。一切都停留在同一个域上,只有一些东西被重新组织和/或重命名。它们的形式如下:
/contact.php
就是现在:
/contact-us.php
使用.htaccess文件,我添加了这一行,这是我最常推荐的那一行:
RedirectMatch 301 /contact.php /contact-us.php
这很好 - 它完成了工作 - 问题是,它还重定向:
有没有办法指定我只想重定向根目录中的contact.php?
RedirectMatch
使用与URL路径匹配的正则表达式。而你的正则表达式/contact.php
只意味着任何包含/contact.php
的URL路径但不仅仅是任何完全/contact.php
的URL路径。因此,使用锚点作为字符串的开头和结尾(^
和$)
:
RedirectMatch 301 ^/contact\.php$ /contact-us.php
这应该做到这一点
RedirectPermanent /contact.php /contact-us.php
redirect 301 /contact.php /contact-us.php
使用重定向匹配规则没有意义,然后必须编写链接以使它们完全匹配。如果你不包括你不必排除!只需使用不带匹配的重定向,然后正常使用链接
你也可以使用 RewriteRule 如果你想要模板匹配和重定向网址的能力。
如果您更喜欢使用 最简单的解决方案 一个问题, RedirectMatch 的替代方案,更基本的 Redirect 指令。
它不使用模式匹配,因此更明确,更容易让其他人理解。
即
<IfModule mod_alias.c>
#Repoint old contact page to new contact page:
Redirect 301 /contact.php http://example.com/contact-us.php
</IfModule>
查询字符串应该结转,因为文档说:
匹配的URL路径之外的其他路径信息将附加到目标URL。