php .htaccess 301 单页重定向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1421068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.htaccess 301 redirect of single page
提问by Dan
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
/联系人.php
is now:
就是现在:
/contact-us.php
/联系我们.php
Using the .htaccess file, I've added this line, which is the one I find recommended most:
使用 .htaccess 文件,我添加了这一行,这是我最推荐的:
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
- /team1/contact.php
- /不存在的文件夹/contact.php
Is there a way of specifying that I only want to redirect the contact.php in the root?
有没有办法指定我只想重定向根目录中的contact.php?
回答by Gumbo
RedirectMatchuses a regular expression that is matched against the URL path. And your regular expression /contact.phpjust means any URL path that contains /contact.phpbut not just any URL path that is exactly /contact.php. So use the anchors for the start and end of the string (^and $):
RedirectMatch使用与 URL 路径匹配的正则表达式。并且您的正则表达式/contact.php仅表示包含/contact.php但不只是包含任何 URL 路径的任何 URL 路径/contact.php。因此,将锚点用于字符串的开头和结尾(^和$):
RedirectMatch 301 ^/contact\.php$ /contact-us.php
回答by duckyflip
This should do it
这应该做
RedirectPermanent /contact.php /contact-us.php
回答by Jon
redirect 301 /contact.php /contact-us.php
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
没有必要使用重定向匹配规则,然后必须编写您的链接,以便它们完全匹配。如果您不包括,则不必排除!只需使用不匹配的重定向,然后正常使用链接
回答by Ryall
You could also use a RewriteRuleif you wanted the ability to template match and redirect urls.
如果您想要模板匹配和重定向 url 的能力,您也可以使用RewriteRule。
回答by JW.
If you prefer to use the simplest possible solutionto a problem, an alternative to RedirectMatchis, the more basic, Redirectdirective.
如果您更喜欢使用最简单的可能解决方案来解决问题,则RedirectMatch的替代方案是更基本的Redirect指令。
It does not use pattern matching and so is more explicit and easier for others to understand.
它不使用模式匹配,因此更明确,更容易被其他人理解。
i.e
IE
<IfModule mod_alias.c>
#Repoint old contact page to new contact page:
Redirect 301 /contact.php http://example.com/contact-us.php
</IfModule>
Query strings should be carried over because the docs say:
查询字符串应该被结转,因为文档说:
Additional path information beyond the matched URL-path will be appended to the target URL.
超出匹配的 URL-path 的其他路径信息将附加到目标 URL。
回答by gurcharan
It will redirect your store page to your contact page
它会将您的商店页面重定向到您的联系页面
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
Redirect 301 /storepage /contactpage
</IfModule>

