jeph perro
jeph perro

Reputation: 6422

How to setup Apache mod_rewrite to redirect all but one subfolder

I've just created a new website and am ready to switch from an my current webserver to a new webserver.

The current webserver will be renamed www2 The new webserver will be known as www

I want to redirect all traffic from www2 to www except for one directory. My directory structure looks like this:

 /var
     /www
         /html
            index.html
            page2.html
            /orange
                 index.html
            ...
            /archive
                 index.html
                 important-page1.html
                 important-page2.html
            /turquoise
                 index.html
            ...

I would like to redirect everything to the equivalent www page

 e.g. www2.mydomain.com/orange/index.html -> www.mydomain.com/orange/index.html
 www2.mydomain.com/turquoise/index.html -> www.mydomain.com/turquoise/index.html

EXCEPT for the /archive folder. I would like users requesting :

www2.mydomain.com/archive/important-page1.html to view the page on www2 and not be redirected.

Do I use mod_rewrite or mod_redirect? And can I set this up in httpd.conf?

Thanks

Upvotes: 2

Views: 7050

Answers (2)

Paul Nicholson
Paul Nicholson

Reputation: 557

Yes, you would need mod_rewrite. Try:

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

Note: The 301 in R=301 is a permanent redirect, you'll need to change it to 302 if you want it to be temporary.

Upvotes: 4

James C
James C

Reputation: 14159

Within the VirtualHost config in httpd.conf (or httpd.conf.d file) for www2.mydomain.com add:

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/archive.*
RewriteRule ^(.*)$ http://www.mydomain.com$1

Upvotes: 1

Related Questions