1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

301 Redirect-What's The Difference?

Discussion in 'Apache' started by ResaleBroker, Aug 28, 2004.

  1. #1
    When redirecting a file, what is the difference between these two methods?

    Method #1:
    redirect 301 old.htm http://www.mydomain.com/new.htm

    Method #2:
    RewriteEngine on
    RewriteBase /
    RewriteRule ^old* http://www.mydomain.com/new.htm$1 [L,R=301]
     
    ResaleBroker, Aug 28, 2004 IP
  2. Will.Spencer

    Will.Spencer NetBuilder

    Messages:
    14,789
    Likes Received:
    1,040
    Best Answers:
    0
    Trophy Points:
    375
    #2
    I have often wondered that myself. :)
     
    Will.Spencer, Aug 28, 2004 IP
  3. Michael

    Michael Raider

    Messages:
    677
    Likes Received:
    92
    Best Answers:
    0
    Trophy Points:
    150
    #3


    I believe Method #2 is preferable because Method #1 is not conditional and the server can get stuck in an endless redirect loop if http://mydomain.com and http://www.mydomain.com are the same URI space i.e. the same files served from the same server account.

    - Michael

     
    Michael, Aug 28, 2004 IP
  4. ResaleBroker

    ResaleBroker Active Member

    Messages:
    1,665
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    90
    #4
    When wouldn't they be?
     
    ResaleBroker, Aug 28, 2004 IP
  5. Owlcroft

    Owlcroft Peon

    Messages:
    645
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    0
    #5
    The differences are these.

    First, in terms of method, they use two distinct Apache modules: mod_alias and mod_rewrite. The redirect form is the mod_alias form, and, as Apache's own docfiles put it, "A more powerful and flexible set of directives for manipulating URLs is contained in the mod_rewrite module."

    But the simpler form is adequate for, well, simpler needs.

    I'm not an expert, and I don't even play one on TV, but I'd say there are differences between the two particular forms cited.

    I am unclear what mod_alias would do given a request for http://www.mydomain.com/junque/old.html -- since that URL contains old.htm, will it trigger a redirect? As I say, I'm not sure.

    But the second, using mod_rewrite, is just defective. The asterisk * cannot be used as in DOS-derived filename specs: these are regular expressions, a curious world with its own rules. The asterisk, in regex-land, simply means "any number of times"--that is, it is an adjective, not a noun. Moreover, even deducing the intent and "translating" the asterisk to .* ("any character, repeated any number of times"), we have the strange situation that regardless of what comes after old, the redirect would be triggered--even if the request is for oldtimestuff.php or oldfriends.gif; moreover, in any event, the resultant translated URL contains an empty variable ($1), since there is no gathering of it in the "if" portion of the rule's if-then structure. Also, the trigger will only be pulled if old* is in the root directory; that may or may not be the intention. A request for http://www.mydomain.com/junque/old.htm would be passed unchanged.

    What I would guess was wanted in mod_rewrite terms would be one of these two, depending on whether or not the redirect should apply to all files so named or only to those in the root directory:

    RewriteEngine on
    RewriteBase /
    RewriteRule ^old.htm$ http://www.mydomain.com/new.htm [L,R=301]

    RewriteEngine on
    RewriteBase /
    RewriteRule ^(.*)/old.htm$ http://www.mydomain.com/$1/new.htm [L,R=301]

    There--if I have this right--the first rewrites http://www.mydomain.com/old.htm to http://www.mydomain.com/new.htm, whereas the second rewrites any call to a file named old.htm to a call to a file in the same directory named new.htm.

    More generally, though, mod_alias is suited for cases in which what you need to trap and transform is easily defined, say a particular file or particular directory; mod_rewrite is for cases in which you need to be more general, and catch and rewrite things with variations--a filename in any directory, or a particular file extension, and so on.

    Wiser heads may clarify any errors here.
     
    Owlcroft, Aug 29, 2004 IP
  6. ResaleBroker

    ResaleBroker Active Member

    Messages:
    1,665
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    90
    #6
    Thank You Eric,
    This goes to show that just because it works doesn't mean its right. :cool:

    Using your example if I wanted to redirect an entire folder in the root directory to my index page [old files with no replacements] does this sound right?
    RewriteRule ^oldfolder$ http://www.mydomain.com [L,R=301]

    What if you leave off the [L,R,=301] is it viewed as a 302?

    Also, if I wanted the old file with any extension [.gif, .html, etc] to be redirected does this sound right?
    RewriteRule ^oldfile(.*)$ http://www.mydomain.com/newfile [L,R=301]

    Finally, do you think it's better to put the redirects in the individual directories or control all redirects through the root directory .htaccess file?

    Thanks for the guidance!
     
    ResaleBroker, Aug 29, 2004 IP
  7. Will.Spencer

    Will.Spencer NetBuilder

    Messages:
    14,789
    Likes Received:
    1,040
    Best Answers:
    0
    Trophy Points:
    375
    #7
    I'm pretty sure that line is defective also, and that the source filename must be defined relative to the DocumentRoot.
     
    Will.Spencer, Aug 29, 2004 IP
  8. Owlcroft

    Owlcroft Peon

    Messages:
    645
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Not quite, to me anyway (and remember my standard disclaimer: I am not an expert, and don't even play one on TV).

    RewriteRule ^oldfolder.*$ http://www.mydomain.com/ [L,R=301]
    The ^ simply means "starting from right after the root-directory slash", and the $ simply means "end of input line". As first given, because of the $ immediately following the dirspec, that, I believe, would redirect only an input URL of the form----but not--

    By putting that nearly invisible little dot -asterisk combination, we regex-specify "followed by any number of arbitrary characters" (which we would normally collect as a variable to pass on, but here do not have to). Note also that this only works as shown if the directory in question is one right off the root directory.

    Yes.

    Not quite--and only if the file is necessarily in the root directory. If it is, I think you want this:

    RewriteRule ^oldfile\.(.*)$ http://www.mydomain.com/newfile.($1)

    That way, we are sure that the file name is oldfile, not, for example, oldfilethingie; the \ "escapes" the following period, making it be seen as an actual period, not a wildcard, so we know the filename part ends there. If the file is not necessarily in the root directory, it gets a bit more complicated:

    RewriteRule ^.*\oldfile\.(.*)$ http://www.mydomain.com/newfile.($1)

    That--I think--will pick out--

    \oldfile.
    --from amidst any incoming URL.

    It's a matter of load. When the server gets a URL call, it works its way down through every directory along the path (the path on the server, through real, physical directories, not URL directories), checking in each for an .htaccess file and, if one is found, adding its content to a building "uber-htaccess"; so the fewer times you make it go through that process, the better. With yet another repeat of my disclaimer, I'd say that there are few and scarce reasons to put a .htaccess file in any lower directory than the root.
     
    Owlcroft, Aug 29, 2004 IP
  9. ResaleBroker

    ResaleBroker Active Member

    Messages:
    1,665
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    90
    #9
    Any difference between these two?:
    RewriteRule ^oldfolder.*$ http://www.mydomain.com/ [L,R=301]
    RewriteRule ^oldfolder(.*)$ http://www.mydomain.com/ [L,R=301]

    What about the [L,R=301]?
     
    ResaleBroker, Aug 29, 2004 IP
  10. Owlcroft

    Owlcroft Peon

    Messages:
    645
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Functionally, no. The parentheses gather what's inside them into a variable that can be referenced in the "then" part of the if-then RewriteRule; but, since you don't need that datum, as you are redirecting everything to the index page, the parens are superfluous but harmless.

    Sheer negligent omission.
     
    Owlcroft, Aug 29, 2004 IP
  11. ResaleBroker

    ResaleBroker Active Member

    Messages:
    1,665
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    90
    #11
    Thanks for the great information! :)
     
    ResaleBroker, Aug 29, 2004 IP
  12. drsuccess

    drsuccess Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Hi, this is great information. My problem is a little different. (Thanks for all your help so far, Eric.) I want to redirect all files (variations of a certain filename that start all the same) to one file. For example:

    I want to redirect all the files in a subfolder that start with "name," as in "name1.htm," "name2.htm," "name3.htm," etc. to the index file within the same directory. But I don't want to redirect the entire subfolder because other files (with other names with different beginnings) within the same directory are still good.

    Help? Thanks.
     
    drsuccess, Feb 12, 2005 IP
  13. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #13
    The amount of work that Apache needs to do in the first case is much less than in the second (i.e. a character-by-character path comparison vs. regular expression match). Consequently, the first method will be faster, in general. For sites with little traffic, this won't matter at all, but heavy-traffic sites may see some difference.

    So, in general, if all you need is just to redirect one URL to another, Redirect will work just fine (there's also a regex version of it - RedirectMatch). If you need to extract parts of the original URL path and use them in the new URL, using some additional logic as well as some server variables (query strings, cookies, etc), mod_rewrite is the way to go.

    J.D.
     
    J.D., Feb 12, 2005 IP