Redirect an entire site with .htaccess but exclude one or more files and directories
Recently we changed the domain name of our website and we need to migrate whole traffic from old website to new website. Our main motive is to migrate our whole traffic to new domain and exclude some folders and files from this redirect. It is clear to us use .htaccess rule to redirect the traffic, but the challenge is I never used such code which migrate whole website except some resources. After searching on internet I got the following which helps me in achieving my task. I am here sharing the same code with you guys.
Exception Rule for files:
RewriteEngine on RewriteCond %{REQUEST_URI} !=/index.php RewriteCond %{REQUEST_URI} !=/content.php RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
This redirects whole traffic to new websites except two mentioned files “index.php” and “content.php”.
Exception Rule for folders:
RewriteEngine on RewriteCond %{REQUEST_URI}!^/test/ RewriteCond %{REQUEST_URI}!^/my-folder/ RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
This redirects whole traffic to new websites except two mentioned folders “test” and “my-folder”. Hope this will help you guys in your work.