Log in

View Full Version : .htaccess redirect for folder only - not files



sniperman
10-02-2011, 01:18 PM
I have moved a directory and its files to a new location on the server and attempted redirects.


# redirect a directory with the filenames preserved
Redirect 301 /old-folder/(.*) http://www.site.com/new-folder/$1

This works for all files after the / but does not redirect http://www.site.com/old-folder/ or http://www.site.com/old-folder (without the forward slash).

rangana
10-02-2011, 01:59 PM
Use:


RedirectMatch 301 ^/old-folder/(.*)$ http://www.site.com/new-folder/$1

sniperman
10-02-2011, 03:15 PM
Thanks. That sorted it out instantly.

The problem now is that on the new directory, when I try to click on any links (it's a blog) then it redirects to the root folder. It needs to be able to show all web pages in the directory and not just the root folder.

Is there a rule to ONLY match one URL folder or path and redirect to another, and not make it a directory-level Directive.

rangana
10-02-2011, 03:41 PM
Place this on the very top:


Redirect 301 /old-folder/ http://www.site.com/new-folder/or_path_to_file.ext

sniperman
10-02-2011, 04:39 PM
That also worked.

I think the problem was that my .htaccess file was in the root directory while the blog (Wordpress) was in a subdirectory. Wordpress generated its own .htaccess file within that folder which took precedence.

Once I removed the Wordpress .htaccess file and placed its Directives in the root directory's .htaccess files, and followed your rule of thumb, the 301 redirects worked across the board.