301 Redirect except 1 directory

Hi everyone,

I am trying to redirect my entire domain except 1 directory - my forum.

After tons of searching online, I have come up with this:

RewriteEngine on 
RewriteCond $1 !^/forums
RewriteRule (.*) http://www.mynewdomain.com/$1 [R=301,L]

The redirect portion of the code works perfectly. The problem is that mydomain.com/forums still redirects to the new domain.

One thing that is important about the redirect is that it needs to preserve the path. That is to say, if someone tries to visit mydomain.com/test it should redirect them to mynewdomain.com/test.

Does anyone know what is wrong with the code above?

Thanks for any help

tt,

What’s wrong with the code is the RewriteCond. It should be RewriteCond %{REQUEST_URI} !^forums/ as there is NO $1 available to the RewriteCond (it’s created by the following RewriteRule).

Regards,

DK

dklynn,

Thank you for the reply. Please correct me if I am wrong. I now have the following code:

RewriteEngine on 
RewriteCond %{REQUEST_URI} !^forums/ 
RewriteRule (.*) http://www.mynewdomain.com/$1 [R=301,L]

No matter what directory a guest tries to visit, they get redirected to the new domain. That includes the /forums directory.

Any idea what could be wrong?

tt,

Apache 1 or Apache 2? If it’s Apache 1, you’ll need a / after the ^ in the RewriteCond.

Are the DocumentRoots colocated? If so, you’ll need to add another RewriteCond to prevent mynewdomain.com from being redirected, too.

RewriteEngine on 
RewriteCond %{HTTP_HOST} !www\\.mynewdomain\\.com [NC]
RewriteCond %{REQUEST_URI} !^/?forums/ 
RewriteRule .? http://www.mynewdomain.com%{REQUEST_URI} [R=301,L]

Regards,

DK