如何转换一个PHP的查询字符串到基于斜线网址? [英] How do I convert a PHP query string into a slash-based URL?

查看:159
本文介绍了如何转换一个PHP的查询字符串到基于斜线网址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是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屋!

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