Web Design Blog

This is where we store some of our Web Development thoughts, tips and tricks, just because we like to share.

Htaccess- 301 Redirect An Entire Directory / Folder

Here’s a simple pierce of code which will permanently redirect a folder / directory. For example, if you have a directory in the following path: http://www.example.com/pictures/, and for whatever reason decide to rename the directory to http://www.example.com/new-pictures/, the following htaccess code will safely redirect all users from /pictures/ to /new-pictures/

Redirect An Entire Directory / Folder with Htaccess

1
2
3
RewriteEngine on
RewriteBase /
RewriteRule ^old_dir/(.*) http://www.example.com/new_dir/ [R=301,L]

The nice thing about this code is that regardless of how deep you go into old_dir, it will always redirect. So literally the entire directory is redirected.

19 Sep 2009 / 6 Comments / htaccess fun / by Maruf

6 Comments

  1. Milan
    30/12/2009
    1

    For me it doesn’t work as in your example. All redirections go to http://www.example.com/new_dir/ no matter what page from http://www.example.com/old_dir/ I open.

    What I wanted is that http://www.example.com/old_dir/somepage.html redirects to http://www.example.com/new_dir/somepage.html

    Any idea for solution?

  2. 22/12/2010
    2

    Yeah, doesn’t work for me either.

    When I use that code it does:

    domain.com/foldertoredirect/crap/

    to

    domain.com/crap/

    I can’t get rid of all the other variables. I want to redirect the entire folder contents and any possible combination to the root domain page.

  3. 22/12/2010
    3

    RewriteEngine on
    RewriteBase /
    RewriteRule ^old_dir/(.*)$ http://www.example.com/new_dir/$1 [R=301,L]

    Is the correct code to be using

  4. 07/01/2011
    4

    Just what I needed to redirect folders in my website.

    Thanks for sharing!

  5. 11/01/2011
    5

    ya it works for me…

  6. shaunm
    02/05/2012
    6

    Many thanks for your help! I have been looking for it very long….finally got it.

    And one more question,
    Can I redirect a site being hosted in a IIS server to an APACHE server with the same codes?

    Thank you……..

Leave a Reply

© 2012 BrightCherry :)