Redirect site to another URL except for a specific directory

You are moving your site to another domain. You want to redirect users to the new domain. But you have a script on a directory that you still want to access on the old domain.

The necessary lines that must be added to your .hatccess file are:

RewriteEngine on
RewriteRule !^(script_directory) https://newdomain.com%{REQUEST_URI} [R=301,L]

If you want to redirect two directories, separate them with | :

RewriteEngine on
RewriteRule !^(directory1|directory2) https://newdomain.com%{REQUEST_URI} [R=301,L]

The code will keep the path – for example olddomain.com/dir1/file1.php will be redirected to newdomain.com/dir1/file1.php

Resources:
RewriteRule Directive

Leave a Reply