Using dynamic mod_rewrite for PHP?

Discussion in 'PHP' started by silence, Jan 7, 2011.

  1. #1
    It's the first time I need mod_rewrite in one of my scripts and I don't know how this could be possible in my case, I've tried several tutorials and some worked but then I had a static page 3k.html instead of link.php?id=3k and it didn't work for any other id than 3k

    I need to have this link: ./link.php?id=$1 to become ./$1/
    So ./link.php?id=3k would become ./3k/

    Any idea what I should put in my .htaccess?

    Thank you :).
    - Silence.

    Edit: this is what I tried:
    RewriteEngine on
    RewriteRule /(.*)/$ /link.php?id=$1
     
    silence, Jan 7, 2011 IP
  2. Minimal Hank

    Minimal Hank Peon

    Messages:
    136
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Options +FollowSymLinks
    RewriteEngine On
    
    RewriteRule ^/([a-zA-Z0-9-_]+)/$ ./link.php?id=$1 [R=301,NC]
    Code (markup):
    ** Had nowhere to test it so let us know if you still have problems with this ..
     
    Minimal Hank, Jan 8, 2011 IP
  3. silence

    silence Peon

    Messages:
    224
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Also worked without those [] in the end :). But now only site/id/ works and not site/id, a slash is required in the end, any idea how to make both work?
     
    silence, Jan 8, 2011 IP
  4. Minimal Hank

    Minimal Hank Peon

    Messages:
    136
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Options +FollowSymLinks
    RewriteEngine On
    
    RewriteRule ^/([a-zA-Z0-9-_]+)(/)?$ ./link.php?id=$1 [R=301,NC]
    Code (markup):
    And again .. haven't tested it but everything should work as expected.
     
    Minimal Hank, Jan 8, 2011 IP
  5. ankit_frenz

    ankit_frenz Active Member

    Messages:
    1,111
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    63
    #5
    silence if you repeatedly need htaccess in future i suggest you get some tuts on regular expressions :)
     
    ankit_frenz, Jan 8, 2011 IP