FAQ: mod_rewrite, 301 redirects, and optimizing Apache.

Discussion in 'Apache' started by Nintendo, Jul 30, 2005.

  1. daytona_k

    daytona_k Well-Known Member

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    116
    #701
    Hi there, i'm sure that this has been asked before, but i can't find it thru the search feature, so, i'll just spit it out.

    What's the difference between next 2 uri's (seo wise), why would i go with one and not the other?

    1. myurl.com/dir/page.html
    2. myurl.com/dir/page

    Did anybody test this on his/her website, and can tell if one could do better in the serps than the other?

    Thanks.
     
    daytona_k, Jul 18, 2007 IP
  2. lummer

    lummer Active Member

    Messages:
    221
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    81
    #702
    FANTASTIC! the rewrite works perfectly, though I didn't quite understand correctly how to do the 301 redirect?
     
    lummer, Jul 18, 2007 IP
  3. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #703
    Redirecting from the dynamic URLs would make the fake URLs go bazerk. Before geting indexed is when mod_rewrite should be done!!

    print "Location:http://www.domain.com/whatever/$FORM{'contact'}/\n\n";

    in perl is the only way I know of.
     
    Nintendo, Jul 18, 2007 IP
  4. lummer

    lummer Active Member

    Messages:
    221
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    81
    #704
    ok thanks.

    do you know how i could do a catchall 301, whereby any 404 requests are 301'd to say the homepage?
     
    lummer, Jul 19, 2007 IP
  5. nQQb

    nQQb Active Member

    Messages:
    332
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #705
    nQQb, Jul 24, 2007 IP
  6. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #706
    Options +FollowSymLinks +Indexes
    RewriteEngine on
    RewriteBase /
    RewriteRule ^rpg/([^.]+)/([^.]+)\.html$ rpg/index.php?group=1&offset=20&thread=$1&id=$2 [L]
     
    Nintendo, Jul 25, 2007 IP
  7. nQQb

    nQQb Active Member

    Messages:
    332
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #707
    Hi nintendo, I tried this and also something else...my php script is a bit complicated and changed urls frequently, I have given up on it...thanks for your help though ...much appreciated :)
     
    nQQb, Jul 26, 2007 IP
  8. trochta

    trochta Peon

    Messages:
    135
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #708
    I can't seem to get this to work for links that are generated with spaces (from old oscommerce):

    here is the link:

    /category.cfm?cat=18&catname=All%20Server%20Rack%20Enclosures

    I simply want to use the link /18/All/Server/Rack/Enclosures/

    but when I try pointing to /18/All/Server/Rack/Enclosures/ I get a 404
     
    trochta, Jul 27, 2007 IP
  9. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #709
    Edit the script to search and replace the spaces to /.
     
    Nintendo, Jul 27, 2007 IP
  10. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #710
    Can I see your mod_rewrite rule(s)?
     
    krt, Jul 27, 2007 IP
  11. dxplus

    dxplus Guest

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #711
    hello.

    currently i only have article id in the url to query the article (since the article id is unique key so that's enough), but in the result url (after mod_rewrite) i want to have the article title instead of article id, much like digg.com url.


    for example:

    real URL:
    www.somepage.com/article.php?id_art=5
    (id_art is the article_id)

    to become:

    www.somepage.com/google_vs_microsoft/
    (google_vs_microsoft is article title)

    is it possible not adding the article title to real URL when i'm querying the article id?

    if it's not possible then should i change the way i'm querying article? (currently only using article_id because that's the unique key in the database)
     
    dxplus, Aug 21, 2007 IP
  12. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #712
    In your table of articles or content items, make the title field an INDEX, and change the queries from "id = " . (int) $_GET['id_art'] to "title = '" . mysql_real_escape_string($_GET['title'])

    The above assumes a very simple PHP/MySQL based application.

    Your .htaccess would be:

    <ifModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)/?$ article.php?title=$1 [L]
    </ifModule>

    Of course, you would also have to change links to point to the new URLs.

    And remember to add code in article.php to send a 404 if the article is not found as this method redirects any URL request to article.php (except for actual files).
     
    krt, Aug 21, 2007 IP
  13. kks_krishna

    kks_krishna Active Member

    Messages:
    1,495
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    80
    #713
    kks_krishna, Aug 21, 2007 IP
  14. VimF

    VimF Well-Known Member

    Messages:
    307
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    118
    #714
    VimF, Aug 22, 2007 IP
  15. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #715
    And if it still doesn't work, add a RewriteBase line.

    RewriteEngine on
    RewriteBase /articles/

    or

    RewriteBase /

    depending on where the .htaccess file it.
     
    Nintendo, Aug 22, 2007 IP
  16. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #716
    No need for full URLs, they just end up with another DNS lookup.

    I also second Nintendo's suggestion to use RewriteBase, e.g.:

    RewriteBase /articles/
    RewriteRule ^index.html$ articles.php [R=301,L]
     
    krt, Aug 23, 2007 IP
  17. Giorgi

    Giorgi Well-Known Member

    Messages:
    234
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    110
    #717
    Hello gr8 ppl overhere.

    I have little problem, I had site with these kind of URLs:

    http://www.something.com/?d=B9F5E8311
    Code (markup):
    Now I am moving to another domain, which will have these kind of URLs:

    http://www.somethingnew.com/index.php?p=download&hash=k9PskIfNgwEj
    Code (markup):
    So, how can I redirect users from first URL to second one? is it possible?
     
    Giorgi, Aug 25, 2007 IP
  18. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #718
    You would have to have one line for every single URL since they have a different ID. Aka, going from B9F5E8311 to k9PskIfNgwEj instead of staying the same.
     
    Nintendo, Aug 25, 2007 IP
  19. Giorgi

    Giorgi Well-Known Member

    Messages:
    234
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    110
    #719
    what if I had same ID s?
     
    Giorgi, Aug 26, 2007 IP
  20. VimF

    VimF Well-Known Member

    Messages:
    307
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    118
    #720
    ...then this will work:

    something.com/.htaccess
    
    Options +FollowSymLinks
    RewriteEngine On
    RewriteCond %{QUERY_STRING} ^d=(.+)
    RewriteRule . http://www.somethingnew.com/index.php?p=download&hash=%1 [L,R=301]
    
    Code (markup):
     
    VimF, Aug 27, 2007 IP