开发者

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

Added

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


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 \/.


First of all you didn't write what framework do you user. I describe what you want to do on Symfony framework

First solution

http://www.symfony-project.org/jobeet/1_4/Doctrine/en/05#chapter_05_route_customizations

  1. Change routes to routing engine understand the "Nice urls"
  2. Change controller's action which is responsible for searching the right record with your's article

You can enhance the solution by declare sluging function and use it directly in routes

Second solution

Use any blogging solution which already supports it - as wrote ceejayoz


You could use PHP and Apache together. Specifically Apache Forcetype. This article explains how to use Forcetype.

Let's say you have a URL like this: http://www.example.com/article/seo-friendly-example

The .htacess file would look like this:

<Files article>
  ForceType application/x-httpd-php 
</Files>

The PHP would look something like this:

<?php
    list(,$slug) = explode("/", $_SERVER['REQUEST_URI']);
?>

The value of $slug would be seo-friendly-example. This would be a key in your database for that article.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜