Domains and DNS

Redirect a domain while keeping certain folders

Reviewed on 2 min read redirecciones htaccess

You can move the website to another domain and keep certain folders on the origin, such as an intranet. To do so, add exceptions to the redirect in the .htaccess file.

This example redirects visits from example.com to example.net, but keeps intranet on the original domain:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/intranet(?:/|$) [NC]
RewriteRule ^(.*)$ https://example.net/$1 [R=302,L]

Add these rules at the top of the .htaccess in the directory linked to the domain or path you are going to redirect. Test them first with the temporary 302 code so the browser does not keep a wrong redirect. Once you have validated the destination and the exclusions, change R=302 to R=301 if the move is permanent.

The rule keeps the path: /articulo will be sent to https://example.net/articulo. If you want to exclude several directories, repeat the condition before the rule and change the name on each line:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/directorio_1(?:/|$) [NC]
RewriteCond %{REQUEST_URI} !^/directorio_2(?:/|$) [NC]
RewriteCond %{REQUEST_URI} !^/directorio_3(?:/|$) [NC]
RewriteRule ^(.*)$ https://example.net/$1 [R=302,L]

Adapt and test the scope

The examples go in the document root of example.com. Replace the origin domain and the destination with your own; in the origin condition, keep the dots escaped. The host name condition stops other domains that share the folder, including the destination, from being redirected.

Test /intranet, /intranet/ and a file inside it: they must stay on the origin. Also test /intranet-publica: it is not the excluded folder and must follow the redirect. Repeat the checks with the real names of your exceptions.

Excluding a folder does not protect it with a password or guarantee that its external resources remain available. Check its CSS, images, forms and authentication; some of them may depend on a path that is being redirected. Keep the previous .htaccess so you can recover the rules if there is an error or a loop.

You may also find useful

Something wrong or out of date? Tell us and we will review it.