Results 1 to 5 of 5

Thread: .htaccess redirect for folder only - not files

  1. #1
    Join Date
    Apr 2009
    Location
    Sydney, Australia
    Posts
    110
    Thanks
    15
    Thanked 1 Time in 1 Post

    Default .htaccess redirect for folder only - not files

    I have moved a directory and its files to a new location on the server and attempted redirects.

    Code:
    # 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).

  2. #2
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Use:
    Code:
    RedirectMatch 301 ^/old-folder/(.*)$ http://www.site.com/new-folder/$1
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  3. The Following User Says Thank You to rangana For This Useful Post:

    sniperman (10-02-2011)

  4. #3
    Join Date
    Apr 2009
    Location
    Sydney, Australia
    Posts
    110
    Thanks
    15
    Thanked 1 Time in 1 Post

    Default

    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.
    Last edited by sniperman; 10-02-2011 at 03:22 PM.

  5. #4
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Place this on the very top:
    Code:
    Redirect 301 /old-folder/ http://www.site.com/new-folder/or_path_to_file.ext
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  6. #5
    Join Date
    Apr 2009
    Location
    Sydney, Australia
    Posts
    110
    Thanks
    15
    Thanked 1 Time in 1 Post

    Default

    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •