htaccess with rewrite

Discussion in 'PHP' started by Gizlen, Jan 4, 2010.

  1. #1
    hi,
    i need your help for my blogging system.

    domain.com/blogs.php?blog_name=USER-NAME
    USER-NAME part can be change and user can use " a-z OR 0-9 OR '-' "


    with using htaccess i want to do, if a visitor visit URL
    domain.com/USER-NAME like that opening domain.com/blogs.php?blog_name=USER-NAME adress



    how can ı do with using mod_write in htaccess?
     
    Gizlen, Jan 4, 2010 IP
  2. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #2
    Options +FollowSymLinks
    RewriteEngine on
    RewriteRule (.*)/$ blogs.php?blog_name=$1

    put the above code in your .htaccess and then it should be accesable at domain.com/USER-NAME
     
    Bohra, Jan 4, 2010 IP
  3. Gizlen

    Gizlen Active Member

    Messages:
    115
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3
    Gizlen, Jan 4, 2010 IP
  4. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #4
    Bohra, Jan 4, 2010 IP
  5. Gizlen

    Gizlen Active Member

    Messages:
    115
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #5
    a blank page open
     
    Last edited: Jan 4, 2010
    Gizlen, Jan 4, 2010 IP
  6. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #6
    No Probs.. lad i could help
     
    Bohra, Jan 4, 2010 IP
  7. Gizlen

    Gizlen Active Member

    Messages:
    115
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #7
    i sent one more pm.
     
    Gizlen, Jan 4, 2010 IP
  8. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #8
    Options +FollowSymLinks
    RewriteEngine on
    RewriteRule (.*)$ blogs.php?blog_name=$1

    use that
     
    Bohra, Jan 4, 2010 IP
  9. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #9
    One thing I would point out is that just with the above code you are going to route EVERYTHING through your blogs.php script (including images, css files etc etc)

    Really you should have a prefix to identify it's meant to be a users profile such as
    /profile/username-here
     
    JAY6390, Jan 4, 2010 IP
  10. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #10
    well i think if something exist it wont execute the mod rewrite rule correct me if i am wrong
     
    Bohra, Jan 4, 2010 IP
  11. Gizlen

    Gizlen Active Member

    Messages:
    115
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #11
    doesnt work :(
     
    Gizlen, Jan 4, 2010 IP
  12. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #12
    Bohra, Jan 4, 2010 IP
  13. Gizlen

    Gizlen Active Member

    Messages:
    115
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #13
    also i prefer use the first one

    url.com/user_name/

    thanks.
     
    Gizlen, Jan 4, 2010 IP
  14. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #14
    That's incorrect Bohra
    You need to change it to something like
    
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^/?(\w-]+)/?$ blogs.php?blog_name=$1 [L]
    
    Code (markup):
     
    JAY6390, Jan 4, 2010 IP
  15. Gizlen

    Gizlen Active Member

    Messages:
    115
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #15
    it works :eek:
     
    Gizlen, Jan 4, 2010 IP
  16. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Cool. Try it without the /? at the beginning of the rewrite rule and see if it still works. If it does, remove it
     
    JAY6390, Jan 4, 2010 IP
  17. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #17
    ohk i was in the perception thats its fine but i guess it isnt after all

    coz files like http://www.woips.com/templates/tictac/img/top_h1_blue.gif still work even if we have the .htaccess
     
    Bohra, Jan 4, 2010 IP
  18. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #18
    Unless you tell the rewrite engine to check if a file exists it will just try and rewrite it anyway
    The psuedo code of the rewrite above is
    -> Turn rewrite Engine on
    -> If File doesn't exist
    -> and folder doesn't exist
    -> rewrite the url if it matches the pattern to blog.php
     
    JAY6390, Jan 4, 2010 IP
  19. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #19
    Alrite so i assume wordpress does this too.. coz they re route everything to index.php
     
    Bohra, Jan 4, 2010 IP
  20. Gizlen

    Gizlen Active Member

    Messages:
    115
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #20
    thanks both of them
     
    Gizlen, Jan 4, 2010 IP