views:

1050

answers:

4

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.

A: 

Try this:

RedirectMatch 302 /\/[^(special)]\/.+/ /collector.html 
RedirectMatch 302 /\/[^(collector\.html)]/ /collector.html
Boris Guéry
This works to get all subdirectories to go to the collector, but doesn't work to redirect files in the root, e.g. example.com/page.html
Tyler McHenry
What's happen ? Does the page is retrieved correctly without any redirect ?
Boris Guéry
Try this on the same line : RedirectMatch 302 /\/[^(special\/)|(collector\.html)].*/ /collector.html
Boris Guéry
+2  A: 

Maybe this? (needs mod_rewrite)

RewriteEngine On
RewriteRule !^(special(/.*)?|collector\.html)$ collector.html [R,L]
Steef
I thought about this but i think he wants to explicitly redirect the user with a 302 code...
Boris Guéry
That's what the [R] flag does
Steef
Had to make a small modification, changed "special/" to "special/?" so that "example.com/special" doesn't redirect, but other than that this worked, thanks!
Tyler McHenry
@BastardSaint: thanks didn't know about this !
Boris Guéry
@Tyler: You might want to make the regex part "!^(special(/.*)?|collector\.html)$" then, because your version will not redirect on URLs like "http://example.com/specialpage.html". I'll edit my answer.
Steef
A: 

Maybe...?

RewriteEngine on
RewriteRule ^(?!special) collector.html [R,NC]
Alex Burr
Never mind... I see that the example above is the solution.
Alex Burr
A: 

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.

aberpaul