puddletown
puddletown

Reputation: 215

how can i redirect all files in one directory to another directory?

I've moved the files on my server to a new directory and would like to 301 redirect all the requests to the files in the new directory.

Say I have:

How do I redirect them to:

without having to redirect each one individually?

Upvotes: 2

Views: 10398

Answers (1)

Diogo Raminhos
Diogo Raminhos

Reputation: 2023

Assuming you're using a server with mod_rewrite, In your root create a file named ".htaccess" and insert the following contents:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^test/(.*) http://domain.com/$1 [R=301,L]
</IfModule>

Note that this will rewrite every single file in the test directory to the domain you inserted.

If you only want to redirect .php files use:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^test/(.*).php http://domain.com/$1.php [R=301,L]
</IfModule>

NOTES: Don't forget to change the domain.com to your domain name, also this type of redirect will make a 301 redirect that is usefull for redirecting PERMANENTLY the url of a file (This will help search engines updating your links).

Upvotes: 4

Related Questions