MonkeyZeus
MonkeyZeus

Reputation: 20747

Apache redirect all child directories except one

So, I have the following URLs:

www.example.com/parent/child1
www.example.com/parent/child2
www.example.com/parent/child3
www.example.com/parent/cool-feature
www.example.com/parent/product
etc...

I need to redirect all children except for child2 to a new domain.


So far I have come up with:

RedirectMatch ^/parent(?!/child2) http://www.new-example.com/parent/

This is working for:

www.example.com/parent/child2

but not for:

www.example.com/parent/child2/

due to the trailing slash


To no avail, I have tried:

RedirectMatch ^/parent(?!/child2/) http://www.new-example.com/parent/
RedirectMatch ^/parent(?!/child2\/) http://www.new-example.com/parent/
RedirectMatch ^/parent(?!/child2)/ http://www.new-example.com/parent/
RedirectMatch ^/parent/(?!child2)/ http://www.new-example.com/parent/

Upvotes: 1

Views: 199

Answers (1)

DocRoot
DocRoot

Reputation: 1201

As mentioned in comments, by itself, your initial directive should work as intended. None of /parent/child2, /parent/child2/ or /parent/child2/anything should be redirected.

However, since you have "other directives", including a "front controller", you might have a conflict. Particularly since your front controller probably uses mod_rewrite and RedirectMatch is a mod_alias directive. mod_rewrite will always execute first, despite the apparent order in .htaccess. But these redirects should execute first, before the front controller.

Try changing this to a mod_rewrite redirect and ensure it is near the top of your .htaccess file, before the front controller.

RewriteRule ^parent(?!/child2) http://www.new-example.com/parent/ [R=302,L]

Upvotes: 2

Related Questions