且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-10-31 13:22:16

这应该适用于两者:

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]