简体   繁体   English

Htaccess 仅重定向顶级 URL

[英]Htaccess redirect only top level URL

I would like to redirect a top level page using.htaccess, but not redirect tier pages.我想使用 .htaccess 重定向顶级页面,但不重定向层级页面。 Let me explain.让我解释。 I currently have this redirect in place:我目前有这个重定向:

Redirect 301 /support /donate

In summary, I want someone to be redirected to /donate when the visit /support .总之,我希望有人在访问/support时被重定向到/donate However, with this rule if someone visits:但是,如果有人访问此规则:

https://www.example.com/support/test

They are redirected to:他们被重定向到:

https://www.example.com/donate/test

I do NOT want them to be redirected in these instances - only if they visit /support or /support/ (note trailing slash).我不希望他们在这些情况下被重定向 - 仅当他们访问/support/support/ (注意尾部斜线)时。

I'm not sure how to do this or if this is possible.我不确定该怎么做,或者这是否可能。 Any ideas?有任何想法吗?

The Redirect directive is prefix-matching, so you will need to use the RedirectMatch directive instead, which matches against a regex. Redirect指令是前缀匹配的,因此您需要改用RedirectMatch指令,它与正则表达式匹配。

For example:例如:

RedirectMatch 301 ^/support/?$ /donate

The above matches /support or /support/ only and redirects to /donate in both cases.以上仅匹配/support/support/并在这两种情况下重定向到/donate Note that the previous Redirect directive would have redirected to /donate or /donate/ depending on whether there was a trailing slash on the initial request.请注意,先前的Redirect指令会重定向到/donate/donate/ ,具体取决于初始请求中是否有尾部斜杠。

You will need to clear your browser cache before testing, since any erroneous 301 (permanent) redirects will have been cached persistently by the browser.您需要在测试前清除浏览器缓存,因为任何错误的 301(永久)重定向都将被浏览器永久缓存。 Test first with 302s to avoid potential caching issues.首先使用 302 进行测试以避免潜在的缓存问题。

Reference:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM