Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

php - htaccess mod_rewrite

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...