Forum Moderators: phranque

Message Too Old, No Replies

Redirecting a whole folder

         

Nick_Hope

5:50 pm on Jul 16, 2005 (gmt 0)

10+ Year Member



I've recently restructured a whole site, changing the name of one of the principal folders and some of the file names within it. Hence some lines from my .htaccess file now look like this:

Redirect permanent /oldfolder/index.htm [mysite.com...]
Redirect permanent /oldfolder/file.htm [mysite.com...]
Redirect permanent /oldfolder/oldfile.htm [mysite.com...]

My problem is that [mysite.com...] is still ranking very highly on Google but gives a 404 missing file error when Google's link is clicked. The cached page shows my old [mysite.com...] but the "index.htm" part of the URL is invisible in Google's stored URL (presumably because it's called index.htm, depsite it not being in the root folder).

How should I solve this, without just waiting for Google's index to sort itself out? I am considering adding one of the following lines?:

Redirect permanent /oldfolder/ [mysite.com...]

But wouldn't this try to link all the files below the oldfolder to corresponding files in newfolder? (i.e. something I really don't want).

Or might the following line work?:

Redirect permanent /oldfolder/ [mysite.com...]

thanks

jdMorgan

4:52 am on Jul 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See RedirectMatch in Apache mod_alias. It will allow you to "drop" the trailing part of the old URL, and redirect to a specific page.

However, for best results from both s usability and search engine ranking viewpoint, you should 301 redirect each old page to one of the following, in order of effectiveness:

  • A direct replacement page.
  • A page that is very similar to the old page and effectively replaces it in most cases.
  • A relevant "subject" or "category" page.
  • Your site map.
  • A page explaining that the requested page is gone, and linking to your site map or index page.
  • Your index page. (Last choice)

    Jim

  • Nick_Hope

    6:29 am on Jul 17, 2005 (gmt 0)

    10+ Year Member



    Thanks Jim. I'm not sure I was totally clear in my first (late night) post about what I'm trying to achieve.

    As you recommend, I've already got a permanent (301) redirect for every changed file in the old structure to its corresponding file in the new structure. But I still get the Google 404 error for the folder because the Google result is pointing to a folder, not a file. And I can't redirect that folder because it's contents are not named like-for-like in the new folder.

    So I guess what I'm looking for in this case is a line which permanently redirects the folder itself, WITHOUT redirecting any of the files in it (because they are redirected one by one on separate lines).

    I checked out your link to Apache's RewriteMatch documentation and I'm afraid I couldn't work out how to achieve what I'm trying to do. Sorry to be dim.

    thanks

    jdMorgan

    3:51 pm on Jul 18, 2005 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    I'd recommend you look into the seemingly-subtle differences between Redirect and RedirectMatch. While Redirect uses prefix-matching, and will "copy" the trailing part of a URL over to the new URL, RedirectMatch uses regular expressions, and so can be used more selectively.

    To demonstrate, let's take an example:


    Redirect 301 /folder/ http://www.example.com/newfolder/
    RedirectMatch 301 ^/folder/ http://www.example.com/newfolder/
    RedirectMatch 301 ^/folder/$ http://www.example.com/newfolder/
    RedirectMatch 301 ^/folder/(.+)$ http://www.example.com/newfolder/xyz/$1

    Given the client-requested URL, "http://www.example.com/folder/foo.html", these different directives would redirect to the following URLs, respectively:

  • http://www.example.com/newfolder/foo.html (foo.html --anything after the prefix-match-- is "copied" into the destination URL).
  • http://www.example.com/newfolder/ (foo.html is "dropped" from destination URL).
  • No redirect, requested URL must end in "/folder/" exactly.
  • http://www.example.com/newfolder/xyz/foo.html ("newfolder" is substituted, "/xyz" is inserted, and "foo.html" is copied into the destination URL).

    This example only illustrates some of the possibilites, since all of the RedirectMatch patterns are start-anchored, but it should be enough to get you going.

    Jim

  • Nick_Hope

    4:18 am on Jul 19, 2005 (gmt 0)

    10+ Year Member



    Thank you very much Jim. Referring back to my original examples I've now achieved what I want with this:

    RedirectMatch 301 ^/oldfolder/$ [mysite.com...]