我如何转换类似的东西
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
Zend 中的动作视图助手 - 解决方法?Action View Helper in Zend - Work around?(Zend 中的动作视图助手 - 解决方法?)
这是将 URI 与 PHP 中用于 MVC 的类/方法匹配的好方Is this a good way to match URI to class/method in PHP for MVC(这是将 URI 与 PHP 中用于 MVC 的类/方法匹配的好方法吗)
我在哪里保存 Zend Framework 中的部分(视图),以便Where do I save partial (views) in Zend Framework, to be accessible for all Views in my App?(我在哪里保存 Zend Framework 中的部分(视图),以
有一个网站的单一入口点.坏的?好的?没问题?Having a single entry point to a website. Bad? Good? Non-issue?(有一个网站的单一入口点.坏的?好的?没问题?)
MVC + 服务层在 Zend 或 PHP 中常见吗?Is MVC + Service Layer common in zend or PHP?(MVC + 服务层在 Zend 或 PHP 中常见吗?)
PHP MVC 方法中的 Hello World 示例Hello World example in MVC approach to PHP(PHP MVC 方法中的 Hello World 示例)