website.com/username

Discussion in 'PHP' started by cancer10, Aug 26, 2009.

  1. #1
    Hi

    I was wondering how do they do that website.com/any-username thing?


    Just like when you signup at twitter, you get u a vanity url like twitter.com/cancer10


    Does anyone know?

    Pls if someone can explain :)


    Thanks
     
    cancer10, Aug 26, 2009 IP
  2. p4n4d0l

    p4n4d0l Peon

    Messages:
    51
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    We can do that using .htaccess for translate som3thin9.com/username = som3thin9.com/member.php?id=username
     
    p4n4d0l, Aug 26, 2009 IP
  3. cancer10

    cancer10 Guest

    Messages:
    364
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    what is the syntax pls?
     
    cancer10, Aug 26, 2009 IP
  4. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #4
    Options FollowSymLinks
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^/]+)/?$ member.php?id=$1& [L,NC]
     
    crivion, Aug 26, 2009 IP
  5. cancer10

    cancer10 Guest

    Messages:
    364
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5

    Hi

    Why have you hard coded the member ID 1?

    can we not have it hardcoded and it will fetch IDs based on the querystring?


    Also, what does [L,NC] mean?
     
    cancer10, Aug 26, 2009 IP
  6. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #6
    If you look correctly, it says $1. This is a reference to the rewriterule before it: ^([^/]+)/? - So $1 contains the member name.

    NC makes this case insensitive. (No Case)
    L makes it the last rule if the regex is matched.
     
    premiumscripts, Aug 26, 2009 IP
  7. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #7
    if you dont understand why you talking about it?
    have you at least tried?
    id=$1 it's a variable which it's not just set to 1 lol
     
    crivion, Aug 26, 2009 IP
  8. cancer10

    cancer10 Guest

    Messages:
    364
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Great, at which line in .htaccess do I have to added the following lines?

     
    cancer10, Aug 26, 2009 IP
  9. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #9
    First of all is ur member.php coded

    u can add it anywhere at the top or bottom doesnt matter much
     
    Bohra, Aug 26, 2009 IP
  10. cancer10

    cancer10 Guest

    Messages:
    364
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Works like a charm!


    Thanks so much for your kind help :)
     
    cancer10, Aug 26, 2009 IP
  11. cancer10

    cancer10 Guest

    Messages:
    364
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Question:

    For multiple pages, do I have to write the code as:


    Options FollowSymLinks
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^/]+)/?$ member.php?id=$1& [L,NC] 
    RewriteRule ^([^/]+)/?$ comments.php?id=$1& [L,NC] 
    RewriteRule ^([^/]+)/?$ question.php?id=$1& [L,NC] 
    Code (markup):

    Pls confirm?
     
    cancer10, Sep 2, 2009 IP
  12. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #12

    That wont work u hve to make ur site like this

    som3thin9.com/username
    som3thin9.com/comments/username
    som3thin9.com/questions/username


    try this

    Options FollowSymLinks
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^/]+)/?$ member.php?id=$1& [L,NC] 
    RewriteRule ^([^/]+)/comments/?$ comments.php?id=$1& [L,NC] 
    RewriteRule ^([^/]+)/questions/?$ question.php?id=$1& [L,NC] 
    Code (markup):
     
    Bohra, Sep 2, 2009 IP
  13. cancer10

    cancer10 Guest

    Messages:
    364
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    You mean I have to create folders to make it work?

    pls tell me why cannot i use this code?

    RewriteRule ^([^/]+)/?$ question.php?id=$1& [L,NC]
    Code (markup):
     
    cancer10, Sep 2, 2009 IP
  14. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #14
    u dont have to create a folder if u use that .htaccess then ur links will be accesable that way

    u cannot use that code because

    for example

    if i access yoursite.com/43 then it will actually be member.php?id=43 the script wont know what ur actually wanting the question part and not the member part
     
    Bohra, Sep 2, 2009 IP
  15. cancer10

    cancer10 Guest

    Messages:
    364
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Oh gotcha!

    So, how can the following be translated/coded by .htaccess
    
    site.com/comments.php?id=10&username=cancer10
    Code (markup):
     
    cancer10, Sep 2, 2009 IP
  16. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #16
    RewriteRule (.*)/(.*)/$ /comments.php?id=$2&username=$1

    som3thin9.com/cancer10/10

    that will make the link in that format
     
    Bohra, Sep 2, 2009 IP
  17. theextelligence

    theextelligence Member

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #17
    Question:

    For multiple pages, do I have to write the code as:
    
    
    RewriteEngine ON
    RewriteRule ^.*/([^/\.]+)/?$ userPage.php?page=$1 [L]
    RewriteRule ^.*/([^/\.]+)/?$ questions.php?page=$1 [L]
    RewriteRule ^.*/([^/\.]+)/?$ comments.php?page=$1 [L]
    Code (markup):
     
    theextelligence, Feb 10, 2010 IP