Welcome to the JaguarPC Community
JaguarPC
Sales: (888) 338-5261
Support: (888)-551-3050
Results 1 to 12 of 12

This is a discussion on Move a directory in a link friendly way in the Shared & Semi-Dedicated forum
I've got some files in a directory on my site. I want to move those files to a different directory. But, I want to do ...

  1. #1
    Loyal Client
    Join Date
    May 2002
    Location
    Wisconsin, USA
    Posts
    568

    Move a directory in a link friendly way

    I've got some files in a directory on my site. I want to move those files to a different directory. But, I want to do it in a friendly way so that when anyone clicks on a link to the older directory (say from a search engine or other outside website), they're pointed to the file in the new directory.

    For example, the files are now in:
    www.example.com/first/second/

    and I want to move them to:
    www.example.com/second/

    I'm pretty much a novice when it comes to .htaccess. If I add the following command to my .htaccess file:

    Redirect /first/second/ http://www.example.com/second/

    Will that do the trick, or do I need a redirect statement for every file?

  2. #2
    JasonM
    Guest
    Try the following in a .htaccess file:

    RewriteEngine On
    RewriteBase /
    RewriteRule ^first/(.*) /second/$1
    A little more complicated but should work. That will work for every file in the directory aswell.

  3. #3
    Loyal Client
    Join Date
    May 2002
    Location
    Wisconsin, USA
    Posts
    568
    Thanks Jason. It just hit me I should have mentioned that I still want to have files in the /first/ directory to be accessible.

    Will the above RewriteRule prevent someone from accessing a file (or another directory) that's in example.com/first/ ?

  4. #4
    JasonM
    Guest
    no.. if you use my code above you put the files in one directory, and it basically cloaks it in a second directory. You only need one copy of the files and you will be able to load them in both.

  5. #5
    Community Leader jason's Avatar
    Join Date
    Sep 2001
    Location
    Rochester, NY
    Posts
    6,003
    Reading Jason's reply (two Jason's in one thread ), it looks like his example will redirect anything in /first to /second. I think what you really want is

    RewriteRule ^first/second/(.*) /second/$1

    (He probably just mis understood your question. OF course, its almost 2 AM and I've been doing a little tonight, so I may be reading this all wrong...)

    And, since there is always more than one way to skin a cat, if you have access to SSH you can also create a symbolic link in the place of the current /first/second directory pointed at the new /second directory. Log in to SSH and enter the following commands (italics are coments, don't enter them):

    cd public_html/first go into the first directory
    mv second .. move the second dir to 1 level up (ie public_html)
    ln -s second ../second create the symlink pointing 'second' at the new top-level second directory

    Either solution will work equally well. The only difference is that the mod_rewrite solution is a redirect. When a browser requests something in /first/second the server will tell it to "look here instead." The symlink method makes an OS level alias to the new location. The server will serve requests for files at /first/second as if the files still physically existed there. The end result of either method will be pretty much unnoticed by the end user. Both methods will still allow you to use /first as you are now.

    Hopefully this isn't too confusing...

    --Jason
    Jason Pitoniak
    Interbrite Communications
    www.interbrite.com www.kodiakskorner.com

  6. #6
    Loyal Client
    Join Date
    May 2002
    Location
    Wisconsin, USA
    Posts
    568
    Jason P, yes I thought the command Jason M suggested would redirect everything in directory /first/ and that is not what I want. I probably should have been a little more clear in my question.

    I think I'll try the RewriteRule that you suggested. I do want to physically move the files (I'm reorganizing my blog), so I assume it would be better to use mod_rewrite than the SSH solution. Also, I'm a little more comfortable working with .htaccess than I am with SSH.

    Thanks Jason and Jason for your help.

  7. #7
    Loyal Client
    Join Date
    May 2002
    Location
    Wisconsin, USA
    Posts
    568
    Okay, I put the following in my .htaccess file:

    RewriteEngine On
    RewriteBase /
    RewriteRule ^journal/archive/(.*) /archive/$1

    Which works, but with one problem. The url in the browser window does not change.

    When people go to mydomain.com/journal/archive/somefile.html I want them to be redirected to mydomain.com/archive/somefile.html

    Since the url in the browser window does not change, any graphic files, css files, etc. that are needed by the browser do not show up (because they are not in the right directories relative to the url in the browser window).

    I hope this makes sense.

  8. #8
    Loyal Client
    Join Date
    May 2002
    Location
    Wisconsin, USA
    Posts
    568
    I think I just answered my own question (yes, Google can be your friend). In case anyone runs into a similar problem, here's what I did.

    I changed the new .htaccess commands to:

    RewriteEngine On
    RewriteBase /
    RewriteRule ^journal/archive/(.*) /archive/$1 [R=301,L]

    From what I understand, the R=301 sends a "Moved Permanently" header, so future hits will reload the page from the new directory.

    I'm not quite sure what the L does though...
    Last edited by JonathanB; 05-22-2004 at 04:30 PM.

  9. #9
    Ron
    Guest
    The "L" says it's the last rule to process. It's optional.

  10. #10
    Yeah, I know a LOT! Vin DSL's Avatar
    Join Date
    Mar 2003
    Location
    Arizona Uplands
    Posts
    10,775
    Originally posted by Ron
    The "L" says it's the last rule to process. It's optional.
    Close...

    The 'L' flag means if it's matched it's the last rule to process, assuming, of course, you don't have any subrequests pending...
    DISCLAIMER Any resemblance between the views expressed above and those of the owners and operators of this system is purely coincidental. Any resemblance between these views and my own are non-deterministic. The existence of Vin DSL is questionable. The existence of views in the absence of anyone to hold them is problematic. The existence of the reader is left as an exercise in the second-order coefficient.

    No Guts, No Story! VinDSL © 2010

  11. #11
    Yeah, I know a LOT! Vin DSL's Avatar
    Join Date
    Mar 2003
    Location
    Arizona Uplands
    Posts
    10,775
    Originally posted by JonathanB
    ...From what I understand, the R=301 sends a "Moved Permanently" header, so future hits will reload the page from the new directory...
    That's a '301' redirect, or a "Moved Permanently" redirect, as you stated . If you're worried about your search engine rankings, you should ALWAYS use 'R=301'. If you don't care about losing your rank on search engines, just use 'R'...
    DISCLAIMER Any resemblance between the views expressed above and those of the owners and operators of this system is purely coincidental. Any resemblance between these views and my own are non-deterministic. The existence of Vin DSL is questionable. The existence of views in the absence of anyone to hold them is problematic. The existence of the reader is left as an exercise in the second-order coefficient.

    No Guts, No Story! VinDSL © 2010

  12. #12
    Ron
    Guest
    Originally posted by Vin DSL
    Close...

    The 'L' flag means if it's matched it's the last rule to process, assuming, of course, you don't have any subrequests pending...
    Correct. I should have been more specific.

    Also, just for completeness, the 301 won't be the returned status unless there's a match, and the new, redirected URI won't be returned, either. The whole section of that line that instructs Apache on what to do if there's a match to the rule won't be invoked unless, of course, there is, of course, a match.

    Also, it might not, of course, actually work at all, unless, of course, it's contained within a .htaccess file with the proper permissions.

    And it has to be on a server that's actually, of course, running a webserver... which has to be connected to a NIC. And plugged in. Don't forget the machine has to have power to operate.

    Just for completeness.

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
  •