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.



brightcherry.co.uk
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?
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.
RewriteEngine on
RewriteBase /
RewriteRule ^old_dir/(.*)$ http://www.example.com/new_dir/$1 [R=301,L]
Is the correct code to be using
Just what I needed to redirect folders in my website.
Thanks for sharing!
ya it works for me…
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……..