如何转换一个PHP的查询字符串到基于斜线网址? [英] How do I convert a PHP query string into a slash-based URL?
问题描述
我是pretty熟练的程序员,但是当涉及到正则表达式和重写,我总的n00b。我想一个URL从
I am a pretty skilled programmer, but when it comes to RegEx and rewriting, I am a total n00b. I want to convert a URL from
http://www.example.com/lookup.php?id=1
到
http://www.example.com/lookup/1/item/
其中,项目是指项目的名称在正被搜索行动的数据库。
where "item" refers to the name of an item in a database that is being looked-up.
我使用LAMP(Linux操作系统,Apache,MySQL和PHP),我不能,因为我的生活,弄清楚如何将URL转换所以他们是搜索引擎友好的。
I'm using LAMP (Linux, Apache, MySQL, PHP) and I cannot, for the life of me, figure out how to convert the URLs so they are SEO friendly.
推荐答案
简单的.htaccess例如:
Simple .htaccess example:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^lookup/([a-z0-9\-]+)/item/?$ /lookup.php?id=$1
</IfModule>
这将匹配任何字母数字任意长度的(也承认破折号)字符串作为ID。您可以通过更改正则表达式来限制这只是数字([0-9] +)
。
This will match any alphanumeric (also will recognise dashes) string of any length as the 'id'. You can limit this to just numeric by changing the regex to ([0-9]+)
.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^lookup/([a-z0-9\-]+)/([a-z0-9\-]+)/?$ /lookup.php?id=$1&view=$2
</IfModule>
这人会匹配 /查找/ 123 /一些文本/
到 /lookup.php?id=123&view=some-text
这篇关于如何转换一个PHP的查询字符串到基于斜线网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!