dan*_*iel 3 php .htaccess mod-rewrite seo
我如何转换类似的东西
me.com/profile/24443/quincy-jones
Run Code Online (Sandbox Code Playgroud)
至
me.com/profile.php?id=24443
或类似的东西
me.com/store/24111/robert-adams
至
me.com/store.php?id=24111
Run Code Online (Sandbox Code Playgroud)
用mod_rewrite?
我可以使用mod_rewrite进行反向转换,还是必须通过PHP解析它?
这应该适用于:
RewriteEngine on
RewriteRule ^([^/]+)/([^/]+).*$ $1.php?id=$2 [L]
Run Code Online (Sandbox Code Playgroud)
说明:
^ - 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
Run Code Online (Sandbox Code Playgroud)
您还可以使其更精确,因此只有两个请求被重写,只有数字被接受为id:
RewriteRule ^((profile|store))/(\d+).*$ $1.php?id=$2 [L]
Run Code Online (Sandbox Code Playgroud)