How can I use mod_rewrite on Apache to make SEO URLs?

Discussion in 'Apache' started by apolodor, Jan 15, 2011.

  1. #1
    All kind of tutorials on the net suggest that can be done. I tried this one: http://www.webreference.com/programming/Mod_Rewrite02 which explains how to "translate" URLs:
    http://www.example.com/library/bookinfo.php?section=biology&bookid=4856
    Code (markup):
    to
    http://www.example.com/library/biology/4856
    Code (markup):

    I need to do the same thing:
    http://localhost/index.php?news=7
    Code (markup):
    to
    http://localhost/news/7
    Code (markup):

    thanks
     
    apolodor, Jan 15, 2011 IP
  2. apolodor

    apolodor Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I tried this code for htaccess but didn't do anything to URLs: Options +FollowSymLinks
    RewriteEngine on
    RewriteRule test(.*)\.htm$ /index.php?news=$1
     
    apolodor, Jan 15, 2011 IP
  3. fardesi

    fardesi Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Is the apache configured to allow override from .htaccess... also... note your .* is doing a greedy match.. you be better off with lazy match... test(.*?)\.htm$
     
    fardesi, Jan 18, 2011 IP
  4. apolodor

    apolodor Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yes, it's enabled.

    this code RewriteRule test(.*?)\.htm$ /index.php?news=$1 didn't do anything either
     
    apolodor, Jan 21, 2011 IP
  5. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #5
    mwasif, Jan 22, 2011 IP
  6. apolodor

    apolodor Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    the first one.
     
    apolodor, Jan 22, 2011 IP
  7. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #7
    Use the following code

    RewriteEngine on
    RewriteRule ^news/([0-9]+)/?$ /index.php?news=$1
     
    mwasif, Jan 22, 2011 IP
  8. apolodor

    apolodor Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    it doesn't do anything. no change in the url
     
    apolodor, Jan 22, 2011 IP
  9. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #9
    There is correction (no need of / before index.php)
    RewriteRule ^news/([0-9]+)/?$ index.php?news=$1

    This will not change the URL in your code (were you expecting this?). After using this code in .htaccess you can access this URL e.g.

    http://localhost/news/7
     
    mwasif, Jan 22, 2011 IP
  10. apolodor

    apolodor Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    yes I'm expecting a new URL - more friendly for SEOs
     
    apolodor, Jan 22, 2011 IP
  11. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #11
    Then you must change your application where you are generating URLs.
     
    mwasif, Jan 22, 2011 IP
  12. Oceanus

    Oceanus Member

    Messages:
    903
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #12
    You may also need to 301 the old URLs to the new mod rewrites.
     
    Oceanus, Feb 9, 2011 IP