简体   繁体   English

SEO友好网址

[英]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 我喜欢SO提出问题的链接的方式像这个问题一样,有链接http://stackoverflow.com/questions/6002203/seo-friendly-url ,其中问题标题为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 ? 我正在创建一个博客,我想在其中以同样的方式给出链接,如何在PHP中做到这一点?
Any suggestion is welcome :) 任何建议都欢迎:)

Table Structure 表结构

  • ID ID
  • Title 标题
  • Tags 标签
  • Category 类别
  • UID UID

Added
I'm using PHP/APACHE and no framework ! 我正在使用PHP / APACHE,没有框架! 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. 您正在寻找的是mod_rewrite ,一个用于URL重写的apache模块。

In your .htaccess file (you might need to make it) put: 在您的.htaccess文件中(您可能需要制作):

<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 . 这意味着当您在幕后转到/blog/10/any-old-bit-of-text/ ,与您访问/blog.php?post=10情况相同。

The ([0-9]+) bit is called a regular expression (or regex), and matches any number. ([0-9] +)位称为正则表达式(或regex),并且与任何数字匹配。 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 我描述了您想在Symfony框架上做什么

First solution 第一个解决方案

http://www.symfony-project.org/jobeet/1_4/Doctrine/en/05#chapter_05_route_customizations 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 您可以通过声明Sluging函数来增强解决方案,并直接在路由中使用它

Second solution 第二解决方案

Use any blogging solution which already supports it - as wrote ceejayoz 使用已经支持它的任何博客解决方案-如ceejayoz所写

You could use PHP and Apache together. 您可以一起使用PHP和Apache。 Specifically Apache Forcetype. 特别是Apache Forcetype。 This article explains how to use Forcetype . 本文介绍了如何使用Forcetype

Let's say you have a URL like this: http://www.example.com/article/seo-friendly-example 假设您有这样的网址: http://www.example.com/article/seo-friendly-example : http://www.example.com/article/seo-friendly-example

The .htacess file would look like this: .htacess文件如下所示:

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

The PHP would look something like this: PHP看起来像这样:

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

The value of $slug would be seo-friendly-example . $slug的值将是seo-friendly-example This would be a key in your database for that article. 这将是该数据库中该文章的关键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM