htaccess mod_rewrite [英] htaccess mod_rewrite

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

问题描述

每当我访问以下页面时,我都会尝试添加一些内容:

<块引用>

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

我希望模组重写将其更改为:

<块引用>

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

当然,ID就是人物名字所在的那一行……

对于那种例子......我已经尝试过使用它,但似乎没有得到大部分的 htaccess 产品,因为我没有使用过 .htaccess真的很多.

解决方案

实际上正好相反:

  1. 在您的网站上,您可以按照自己希望的方式编写所有链接.例如 http://www.site.com/character/Jim_Carrey

  2. 在您的数据库中,您添加一个字段,通常称为弹头"或post_slug"指的是Jim_Carrey" url 的一部分.它必须成为一个独特的元素,很重要的一个主键.所以请确保你有一个可以照顾的功能基于a创建slug给定的字符串(帖子标题示例)并确保没有重复.

  3. 然后在你的 .htaccess(在根文件夹上)你做类似的东西

    RewriteEngine OnRewriteRule ^character/([a-z0-9_\-]+)/$ character.php?slug=$1 [L,NC]

  4. 最后,在你的 character.php脚本,你做一个数据库查询不是反对身份证,但反对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天全站免登陆