Andy Holmes
Andy Holmes

Reputation: 8077

htaccess redirect if URI matches and doesn't have anything after it

I'm having a bit of an issue with a 301 redirect to fix some client site requirements.

Here's the scenario:

If I go to website.com/case-studies I would like it to redirect to website.com/case-studies/any/any/any, however, if we have something like website.com/case-studies/technology/any/any it shouldn't redirect.

I have tried Redirect 301 /case-studies /case-studies/any/any/any but this basically put me in a loop of /any * 30 appearing in the address bar.

Upvotes: 1

Views: 154

Answers (1)

anubhava
anubhava

Reputation: 785156

You will need to a regex based directive and use anchors. So use RedirectMatch instead of Redirect:

RedirectMatch 301 ^/case-studies/?$ /case-studies/any/any/any

Regex ^/case-studies/?$ will match /case-studies or /case-studies/ but it won't match anything beyond that.

Make sure to clear your browser cache when testing this change.

Upvotes: 1

Related Questions