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.

Boris Guéry
  • 47,316
  • 8
  • 52
  • 87
Tyler McHenry
  • 74,820
  • 18
  • 121
  • 166

4 Answers4

4

Maybe this? (needs mod_rewrite)

RewriteEngine On
RewriteRule !^(special(/.*)?|collector\.html)$ collector.html [R,L]
Steef
  • 33,059
  • 4
  • 45
  • 36
  • I thought about this but i think he wants to explicitly redirect the user with a 302 code... – Boris Guéry May 29 '09 at 14:03
  • 1
    That's what the [R] flag does – Steef May 29 '09 at 14:16
  • 1
    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 May 29 '09 at 14:56
  • 1
    @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 May 29 '09 at 19:30
2

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
  • 437
  • 3
  • 6
0

Try this:

RedirectMatch 302 /\/[^(special)]\/.+/ /collector.html 
RedirectMatch 302 /\/[^(collector\.html)]/ /collector.html
Boris Guéry
  • 47,316
  • 8
  • 52
  • 87
0

Maybe...?

RewriteEngine on
RewriteRule ^(?!special) collector.html [R,NC]
Alex Burr
  • 51
  • 3