Tyler McHenry
Tyler McHenry

Reputation: 76640

How to use htaccess to redirect all but one subdirectory

It's been a while since I've messed with .htaccess and I can't seem to get this quite right. I have a site, say example.com, where I want example.com/* to redirect to example.com/collector.html except for URLs under the subdirectory example.com/special, which I want to continue working.

For example:

I would have thought that something like

RedirectMatch 302 ^/[^(special)]/.* /collector.html
RedirectMatch 302 ^/[^(collector.html)/]* /collector.html

would do the trick, but it doesn't seem to work the way I want it to.

Upvotes: 4

Views: 7881

Answers (4)

aberpaul
aberpaul

Reputation: 447

This works fine for me on Apache 2.2.13 where I recreated the directory structure you described.

RedirectMatch 302 /(?!special)  http://www.example.com/collector.html

The pattern in the brackets is saying do NOT match if the URI contains the string 'special'.

Also mod rewrite is not always available and in this case is not needed.

Upvotes: 2

Steef
Steef

Reputation: 34665

Maybe this? (needs mod_rewrite)

RewriteEngine On
RewriteRule !^(special(/.*)?|collector\.html)$ collector.html [R,L]

Upvotes: 4

Alex Burr
Alex Burr

Reputation: 51

Maybe...?

RewriteEngine on
RewriteRule ^(?!special) collector.html [R,NC]

Upvotes: 0

Boris Guéry
Boris Guéry

Reputation: 47585

Try this:

RedirectMatch 302 /\/[^(special)]\/.+/ /collector.html 
RedirectMatch 302 /\/[^(collector\.html)]/ /collector.html

Upvotes: 0

Related Questions