4

I'm going to try and explain the situation I'm in so the problem is a little more understandable first I think. I'm basically tryng to redirect an old website to a new one using:

RedirectMatch permanent ^/old-website/.*        /new-website/

This works to redirect the whole of the old website to the new website. I've since found out from the client that a folder from their old website still need to be accessible so I added an exception:

RedirectMatch permanent ^/old-website/(?!foo).*        /new-website/

That also works fine, but I've now been told there are multiple folders they still need to access. The reason these folders need accessing is that they've been printed in documents that have circulated for the last few years so moving them to a new place would mean anyone looking at one of these printed documents would get a broken link.

I can add multiple exceptions using this syntax:

RedirectMatch permanent ^/old-website/(?!foo)(?!bar/).*        /new-website/

However, what I want to know is there a better/neater way of doing this. The other thing to bear in mind is that the /old-website is an alias of /~user-account.

I haven't had to do redirects like this before so my question is this: Is adding exceptions to a RedirectMatch rule like this safe and reliable and should I look out for problems?

Bendihossan
  • 218
  • 3
  • 7

1 Answers1

3

This is where mod_rewrite comes in. As anyone will tell you, mod_rewrite is voodoo, but it's very powerful and is exactly what you need for this situation.

The base redirect is sound (and very reliable)

RedirectMatch permanent ^/old-website/.*        /new-website/

You can then move your old files into the new website as well and specify a couple of rewrite directives that ensure the old URLs will still work:

RewriteRule ^/old-website/foo/(.*)    /new-website/new-foo/$1    [L,R=301]
RewriteRule ^/old-website/bar/(.*)    /new-website/new-bar/$1    [L,R=301]

This will match any requests for the old URLs, rewrite them to the new location, while preserving the remainder of the path: $1 is replaced with the contents of (.*) in this case. So as long as you maintain the filenames and path structure in the new location, then all your old links will still work.

Note that mod_rewrite instructions always happen before redirects. Bear this in mind when creating your ruleset.

For more details, I'll refer you to the this excellent question.

EDIT:

If you don't have access to mod_rewrite, then you should get away with the following:

RedirectMatch permanent ^/old-website/(?!(foo|bar)/).*        /new-website/

Though if you've got more folders than that, then the match line gets to be a bit unwieldy.

SmallClanger
  • 8,947
  • 1
  • 31
  • 45
  • Hi SmallClanger, thanks for that. Under ordinary circumstances that sounds like a good solution. However we have a bit of a totalitarian sysadmin who won't allow us to use `mod_rewrite.` The other problem is that the path structure is different on the two websites and the old files can't be moved to the new website (for various political reasons I won't go into). Thanks for your help and that useful link :) – Bendihossan May 11 '11 at 10:15
  • In that case, see my edit. I've tried this out on a test box, and it works as a negative match for as many folders as you need. – SmallClanger May 11 '11 at 10:41