Search code examples
.htaccessdirectory-structure

How can I redirect an entire sub-directory and its contents to the same directory in another domain using .htaccess?


How can I redirect an entire sub-directory and its contents to the same directory in another domain using .htaccess?

For example, all files here:

www.olddomain.com/directory1

Automatically 301 redirect to:

www.newdomain.com/directory1

So...

www.olddomain.com/directory1 -> www.olddomain.com/directory1

www.olddomain.com/directory1/file1.php -> www.newdomain.com/directory1/file1.php

www.olddomain.com/directory1/directory2/file2.php -> www.newdomain.com/directory1/directory2/file2.php

Solution

  • You have tagged this question with ".htaccess", so I assume that you are using the Apache web server.

    You can do the redirection with mod_rewrite. Please read over its documentation and the rewrite guide, especially the latter.

    The following two lines in your virtual host configuration for olddomain.com should work. They could even work on a .htaccess file inside the root directory of olddomain.com, if you have set the rights accordingly (approximately AllowOverride All).

    RewriteEngine on
    RewriteRule   ^directory1(.*)          http://www.newdomain.com/directory1$1 [R=301]