puddletown puddletown - 1 year ago 70
Apache Configuration Question

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:

  • domain.com/test/apples.php

  • domain.com/test/oranges.php

  • domain.com/test/bananas.php

How do I redirect them to:

  • domain.com/apples.php

  • domain.com/oranges.php

  • domain.com/bananas.php

without having to redirect each one individually?

Answer Source

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]

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]

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).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download