网站重新设计后,我有几个页面需要重定向。所有内容都保留在同一个域中,只有一些内容被重新组织和/或重命名。它们的形式为:
/contact.php
现在为:
/contact-us.php
使用 .htaccess 文件,我添加了这一行,这是我发现最推荐的一行:
RedirectMatch 301 /contact.php /contact-us.php
这基本上很好 - 它可以完成工作- 问题是,它还重定向:
- /team1/contact.php
- /non-existant-folder/contact.php
有没有办法指定我只想重定向根目录中的 contact.php ?
After a site redesign, I've got a couple of pages that need to be redirected. Everything is staying on the same domain, just a couple of things have been reorganised and/or renamed. They are of the form:
/contact.php
is now:
/contact-us.php
Using the .htaccess file, I've added this line, which is the one I find recommended most:
RedirectMatch 301 /contact.php /contact-us.php
This is mostly fine - it does the job - the problem is, it also redirects:
- /team1/contact.php
- /non-existant-folder/contact.php
Is there a way of specifying that I only want to redirect the contact.php in the root?
发布评论
评论(7)
RedirectMatch
使用正则表达式与 URL 路径相匹配。并且您的正则表达式/contact.php
只是表示 任何包含/contact.php
的 URL 路径,而不仅仅是 任何包含/contact.php
的 URL 路径正是/contact.php
。因此,使用锚点作为字符串的开头和结尾(^
和$)
:RedirectMatch
uses a regular expression that is matched against the URL path. And your regular expression/contact.php
just means any URL path that contains/contact.php
but not just any URL path that is exactly/contact.php
. So use the anchors for the start and end of the string (^
and$)
:这应该可以做到
This should do it
使用重定向匹配规则然后必须编写链接以使其完全匹配是没有意义的。如果不包含,则不必排除!只需使用不匹配的重定向,然后正常使用链接
There is no point using the redirectmatch rule and then have to write your links so they are exact match. If you don't include you don't have to exclude! Just use redirect without match and then use links normally
如果您希望能够进行模板匹配,您还可以使用 RewriteRule和重定向网址。
You could also use a RewriteRule if you wanted the ability to template match and redirect urls.
如果您更喜欢使用最简单的解决方案来解决问题,可以使用RedirectMatch 是更基本的 重定向 指令。
它不使用模式匹配,因此更明确且更易于其他人理解。
即
查询字符串应该被保留,因为文档说:
If you prefer to use the simplest possible solution to a problem, an alternative to RedirectMatch is, the more basic, Redirect directive.
It does not use pattern matching and so is more explicit and easier for others to understand.
i.e
Query strings should be carried over because the docs say:
它将把您的商店页面重定向到您的联系页面
It will redirect your store page to your contact page
如果您需要为所有 URL 创建动态重写。
例如,通过 301 重写从 .php 扩展名到 .html,您可以使用以下命令:
In case you need to create a dynamic rewrite for all of your URLs.
For example, from .php extension to .html with a 301 rewrite, you can use this: