我为一个客户完成了新网站,现在我正在 .htaccess 文件中进行所有重定向。我对下面几行中的源 URL 部分感到有些困惑。 这两行的工作方式是否相同?
Redirect 301 /shop/contact-us http://www.example.com/contact-us/
Redirect 301 /shop/contact-us/ http://www.example.com/contact-us/
最佳答案
如果你想用一些可选的尾部斜杠重定向 /shop/contact-us
到 http://www.example.com/contact-us
,那么 Redirect
指令不是很合适。使用 RedirectMatch
指令改为:
RedirectMatch 301 "^/shop/(contact\-us)/?" http://www.example.com/$1/
在哪里
^
是一个 anchor ,意思是“行的开始”;/?
匹配零个或一个斜杠字符;(contact\-us)
是一个捕获组(由$1
引用)
注意,正则表达式只匹配前缀,因为只使用了 ^
anchor 。您可以使用 $
(行尾) anchor 以使表达式更严格,例如:
RedirectMatch 301 "^/shop/(contact\-us)/*$" http://www.example.com/$1/
其中 /*
表示零个或多个斜杠。
关于php - .htaccess 中的这两个重定向有什么区别以及如何为这两个编写单个重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41805755/