简体   繁体   中英

Htaccess redirect only top level URL

I would like to redirect a top level page using.htaccess, but not redirect tier pages. 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 . 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).

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.

For example:

RedirectMatch 301 ^/support/?$ /donate

The above matches /support or /support/ only and redirects to /donate in both cases. 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.

You will need to clear your browser cache before testing, since any erroneous 301 (permanent) redirects will have been cached persistently by the browser. Test first with 302s to avoid potential caching issues.

Reference:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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