.htaccess 301 重定向路径和所有子路径
我希望访问例如 www.thisdomain.com/docs/path1/path2 以重定向到 www.thatdomain.com/path1/path2
(请注意,文档不是新路径的一部分)
我在 www.thisdomain 上有以下内容。 com:
RewriteEngine on
RewriteRule ^docs/* http://www.domain.com/ [R=301,L]
如果我访问 www.thisdomain.com/docs,它会定向到 www.thatdomain.com,但如果我访问像 www.thisdomain.com/docs/path1/path2 这样的子路径,它会失败。重定向是否可以拦截子路径访问并根据我的需要进行重定向?如果是这样,有什么指示吗?
谢谢。
I want accesses to e.g. www.thisdomain.com/docs/path1/path2 to redirect to www.thatdomain.com/path1/path2
(Note that docs is not a part of the new path)
I have the following on www.thisdomain.com:
RewriteEngine on
RewriteRule ^docs/* http://www.domain.com/ [R=301,L]
If I access www.thisdomain.com/docs, it directs to www.thatdomain.com, but if I access a child-path like www.thisdomain.com/docs/path1/path2 it fails. Is it possible for the redirect to intercept the child-path access and redirect as I need? If so, any pointers?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于正则表达式,* 表示前一个原子的任意数量,它将匹配 /docs 和 /docs/。试试这个:(
QSA
是查询字符串追加,因此/docs/foo?bar=baz
不会丢失?bar=baz
。 )With regular expressions, * means any number of the previous atom, which will match /docs and /docs/. Try this:
(
QSA
is query string append, so/docs/foo?bar=baz
won't lose the?bar=baz
.)根据 第一个的“使用 htaccess 重定向将旧域重定向到新域”部分我发现搜索“htaccess重定向”(不带双引号)的Google结果,这段代码就足够了:
根据他们对该技术的描述,它将重定向 .htaccess 文件递归放置的目录(包括任何子路径),正如您所希望的那样。当然,需要存在 mod_rewrite 才能使重写指令发挥作用。
According to section "Redirect Old domain to New domain using htaccess redirect" of the first Google result which I found searching for "htaccess redirect" (without the double quotes), this piece of code will suffice:
According to their description of the technique, it will redirect the directory the .htaccess file is placed in recursively (including any child paths), just as you intend. Of course, mod_rewrite needs to be present for the rewrite directives to work.