使用 apache 的 mod_rewrite 解析 SEO 友好的 URL

using apache#39;s mod_rewrite to parse SEO friendly URL#39;s(使用 apache 的 mod_rewrite 解析 SEO 友好的 URL)
本文介绍了使用 apache 的 mod_rewrite 解析 SEO 友好的 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

您还可以使其更精确,以便仅重写两个请求并且仅接受数字作为 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 解析 SEO 友好的 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Codeigniter htaccess to remove index.php and www(Codeigniter htaccess 删除 index.php 和 www)
htaccess mod_rewrite part of url to GET variable(htaccess mod_rewrite url 的一部分到 GET 变量)
Replacing a querystring parameter value using mod_rewrite(使用 mod_rewrite 替换查询字符串参数值)
.htaccess in subdirectory #39;overriding#39; parent htaccess(子目录“覆盖父 htaccess 中的 .htaccess)
How to rewrite SEO friendly url#39;s like stackoverflow(如何像stackoverflow一样重写SEO友好的url)
Is it okay to have a very long .htaccess file?(有一个很长的 .htaccess 文件可以吗?)