.htaccess 301 redirect syntax differences...which format is better?

Discussion in 'Apache' started by MrThePlague, Dec 7, 2007.

  1. #1
    When doing research on 301 redirects in Apache, I've noticed there are two ways you can accomplish the redirect in the .htaccess file...

    Option 1
    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^old.htm$ http://www.new.com/ [R=301,L]

    Option 2
    redirect 301 /old.htm http://www.new.com/

    Are there advantages / disadvantages to either? I'd be inclined to go with the second option for the simplicity, but perhaps I'm missing something? Any comments would be great, thanks!
     
    MrThePlague, Dec 7, 2007 IP
  2. powerspike

    powerspike Peon

    Messages:
    312
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I prefer to go with Option 1, as if you start using other rewrite rules for your website (or other websites), it'll be in the same format, where as with option 2, it's a different format to the rest of the rewrite rules.
     
    powerspike, Dec 8, 2007 IP
  3. Ladadadada

    Ladadadada Peon

    Messages:
    382
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I suspect that the first option gives you greater possibilities with how you want to control the rewrite. For instance, you can specify [R=301, NC, QSA, L] if you want the rewrite to be case-insensitive, have anything after the question mark appended and be the last one. I don't think you can do that with Redirect.
     
    Ladadadada, Dec 8, 2007 IP
  4. Jean-Luc

    Jean-Luc Peon

    Messages:
    601
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Option 2 is easier to read and understand for the webmaster. It is also "easier" to process for the web server. It does not allow wildcard characters and regular expressions.

    My conclusion is:
    - use option 2 when possible
    - use option 1 otherwise

    Jean-Luc
     
    Jean-Luc, Dec 8, 2007 IP
  5. MrThePlague

    MrThePlague Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    So if I'm trying to get everything under a parent directory to go to one URL (basically avoiding a canonical URL issue) would this be the most streamlined code for that?

    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^test$ http://www.example.com/test/ [R=301]
    RewriteRule ^test/$ http://www.example.com/test/ [R=301]
    RewriteRule ^test/index(.*)$ http://www.example.com/test/ [R=301,L]
     
    MrThePlague, Dec 10, 2007 IP
  6. Jean-Luc

    Jean-Luc Peon

    Messages:
    601
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #6
    This is better:
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^test$ http://www.example.com/test/ [R=301]
    RewriteRule ^test/index(.*)$ http://www.example.com/test/ [R=301,L]
    
    Code (markup):
    This creates an infinite loop:
    RewriteRule ^test/$ http://www.example.com/test/ [R=301]
    
    Code (markup):
    Jean-Luc
     
    Jean-Luc, Dec 10, 2007 IP