htaccess的的mod_rewrite [英] htaccess mod_rewrite

查看:206
本文介绍了htaccess的的mod_rewrite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图把一些与此,每当我去一个网页,如:

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

我希望mod-rewrite将其改为:

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

这当然,该ID是角色名的行...

对于类的例子......我试着用它来工作,但似乎并没有得到大多数企业生产的htaccess的,因为我还没有与的.htaccess 很多真的。

解决方案

它实际上是周围的其他方法:

  1. 在你的网站,你写的所有的链接,您要他们看的方式。例如 http://www.site.com/character/Jim_Carrey

  2. 在你的数据库,你添加一个字段, 通常被称为塞或 post_slug,它指的是 Jim_Carrey部分的URL,它必须 是一个独特的元素,很多在路上 的主键。所以一定要确保你 有一个功能,确实照顾 创建蛞蝓的基础上 给定的字符串(文章标题为 例如),并确保不存在 复制的。

  3. 然后在你的.htaccess(根文件夹),你做的 像

      RewriteEngine叙述上
    重写规则^字符/?([A-Z0-9 _ \  - ] +)/ $ character.php塞= $ 1 [L,NC]
     

  4. 最后,在你的character.php 脚本,你做一个数据库查询不 对ID,而是针对 post_slug领域。

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.

解决方案

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.

这篇关于htaccess的的mod_rewrite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆