How to configure Apache mod_rewrite to redirect all but one subfolder

I just created a new website and am ready to move from the current web server to the new web server.

The current web server will be renamed to www2 The new web server will be known as www

I want to redirect all traffic from www2 to www except 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 to request:

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

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

thank

+2


source to share


2 answers


Yes, you need mod_rewrite. Try:

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

      



Note: 301 R=301

is a permanent redirect , you will need to change it to 302 if you want it to be temporary.

+4


source


Inside VirtualHost

config in httpd.conf file (or httpd.conf.d

) for www2.mydomain.com add:



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

      

+1


source







All Articles