使用Apache的mod_rewrite解析搜索引擎友好的URL [英] using apache's mod_rewrite to parse SEO friendly URL's
本文介绍了使用Apache的mod_rewrite解析搜索引擎友好的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要如何转换像
me.com/profile/24443/quincy-jones
到
me.com/profile.php?id=24443
之类的东西。
me.com/store/24111/robert-adams
到
me.com/store.php?id=24111
与mod_rewrite的?
with mod_rewrite?
我可以做相反的转换,以及与mod_rewrite的,否则我将不得不通过PHP解析呢?
Can I make the reverse conversion as well with mod_rewrite, or would I have to parse it through PHP?
推荐答案
这应该为:
RewriteEngine on
RewriteRule ^([^/]+)/([^/]+).*$ $1.php?id=$2 [L]
说明:
^ - beginning of the string
([^/]) - first group that doesn't contain /
will match both 'profile' and 'store'
will also be referenced by $1 later
/ - first slash separator
([^/]) - second group, id in your case
will be referenced by $2
.* - any ending of the request uri
$ - end of request string
您也可以使之更precise所以只有两个要求被改写,只有数字被接受为ID:
You can also make it more precise so only the two request are rewritten and only digits are accepted as id:
RewriteRule ^((profile|store))/(\d+).*$ $1.php?id=$2 [L]
这篇关于使用Apache的mod_rewrite解析搜索引擎友好的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文