How to use htaccess to redirect all but one subdirectory

Tyler McHenry picture Tyler McHenry · May 29, 2009 · Viewed 7.9k times · Source

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:

  • example.com/page.html should redirect to example.com/collector.html
  • example.com/foo/bar should redirect to example.com/collector.html
  • example.com/special/page.html should not redirect

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.

Answer

Steef picture Steef · May 29, 2009

Maybe this? (needs mod_rewrite)

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