Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to put something with this, whenever I go to a page like:

http://www.example.com/character.php?id=3

I want the mod rewrite to change it to:

http://www.example.com/character/Jim_Carrey

Which of course, the ID is the row of the character name...

For that kind of example... I've tried to work with it, but don't seem to get most of the production of htaccess because I haven't worked with .htaccess a lot really.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
190 views
Welcome To Ask or Share your Answers For Others

1 Answer

It's actually the other way around:

  1. On your website, you write all your links the way you want them to look. For instance http://www.site.com/character/Jim_Carrey

  2. In your database, you add a field, commonly called "slug" or "post_slug" which refers to the Jim_Carrey" part of the url. IT MUST BE A UNIQUE ELEMENT, much in the way of a primary key. So make sure you have a function that does take care of creating the slug based on a given string (the post title for example) and making sure there is no duplicate.

  3. Then in your .htaccess (on the root folder) you do something like

    RewriteEngine On
    RewriteRule ^character/([a-z0-9_-]+)/$ character.php?slug=$1 [L,NC]
    
  4. Finally, in your character.php script, you do a database query not against the ID, but against the post_slug field.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...