+3 votes
in Web & Google by (73.8k points)

Except for some folders in the old domain, I want to redirect all pages and folders of my old domain to a new domain.

E.g. I do not want a redirection for folder1 and folder2, but other folders should be redirected.

  • olddomain.com/folderx/ -> newdomain/folderx/
  • olddomain.com/folderx/abc.php -> newdomain/folderx/abc.php
  • olddomain.com/folder1/ -> olddomain/folder1/
  • olddomain.com/folder2/ -> olddomain/folder2/

What changes do I need to make in the .htaccess file for such redirection?

1 Answer

+2 votes
by (348k points)
selected by
 
Best answer

You need to check REQUEST_URI for the folder name you do not want to redirect. If the REQUEST_URI contains the folder name, do not redirect.

You can put the following code in the .htaccess file of your old domain if you do not want a redirection for folder1 and folder2. The following code redirect pages to the "https://www." version. You can edit it as per your settings. Also, if you are not willing to do the permanent redirection, you can use 302 in place of 301.

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/(folder1|folder2)/
RewriteRule ^(.*)$ https:/ /www.newdomain.com/$1 [R=301,L]


...