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

Categories

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

php - SEO Friendly URL

I love the way SO gives link to question Like this question have the link http://stackoverflow.com/questions/6002203/seo-friendly-url where the question title is seo-friendly-url

I'm creating a blog where i want to give the link in the same way SO do, how to do that in PHP ?
Any suggestion is welcome :)

Table Structure

  • ID
  • Title
  • Tags
  • Category
  • UID

Added
I'm using PHP/APACHE and no framework ! I dont want to use any blog, want to create my own

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

I'm not sure why people are being so deliberately obtuse here...

What you are looking for is mod_rewrite, an apache module for URL rewriting.

In your .htaccess file (you might need to make it) put:

<IfModule mod_rewrite.c>
RewriteEngine on

RewriteRule ^blog/([0-9]+)/.*$ /blog.php?post=$1 [L]
</IfModule>

This means when you go to /blog/10/any-old-bit-of-text/ behind the scenes it is identical to if you visited /blog.php?post=10.

The ([0-9]+) bit is called a regular expression (or regex), and matches any number. The .* means match anything. The ^ anchors to the start of the query and the $ anchors to the end. slashes (/) are escaped as /.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...