Main Content

Forwarding a whole domain, except for a few directories - Apache http server

Archive - Originally posted on "The Horse's Mouth" - 2012-09-17 11:02:59 - Graham Ellis

I was set a puzzle via email last week - how to redirect an entire domain to a new server / domain, apart from three specific directories / folders which are to remain on the old server.

Clearly, this is a task for mod_rewrite, and the natural and instrincive reaction is to write a regular expression match to somehow select all domains except those which need to remain on the current server, and to redirect them. Alas, that will proove to be easier said than done. There turns out to be a much easier way to do it ...

1. Rewrite each of the directories that's to be left on the current server, making it a last rule with the [L] option:

  RewriteRule ^lv/(.*) lv/ [L]
  RewriteRule ^lt/(.*) lt/ [L]
  RewriteRule ^ee/(.*) ee/ [L]


2. Then rewrite all remaining URLs to the new server:

  RewriteRule (.*) http://newservername.com/

You may choose to add [R=301] on the end of this if you wish to indicate to your visitor that your change is a permanent one.