.htaccess 301 重定向路径和所有子路径

发布于 2024-10-02 06:38:28 字数 426 浏览 1 评论 0原文

我希望访问例如 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぃ弥猫深巷。 2024-10-09 06:38:28

对于正则表达式,* 表示前一个原子的任意数量,它将匹配 /docs 和 /docs/。试试这个:(

RewriteEngine on
RewriteRule ^docs$ http://www.domain.com/ [R=301,L,QSA]
RewriteRule ^docs/(.*) http://www.domain.com/$1 [R=301,L,QSA]

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:

RewriteEngine on
RewriteRule ^docs$ http://www.domain.com/ [R=301,L,QSA]
RewriteRule ^docs/(.*) http://www.domain.com/$1 [R=301,L,QSA]

(QSA is query string append, so /docs/foo?bar=baz won't lose the ?bar=baz.)

病女 2024-10-09 06:38:28

根据 第一个的“使用 htaccess 重定向将旧域重定向到新域”部分我发现搜索“htaccess重定向”(不带双引号)的Google结果,这段代码就足够了:

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]

根据他们对该技术的描述,它将重定向 .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:

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文