用mod_rewrite重写URL

Web*_*bby 2 .htaccess mod-rewrite url-rewriting

我正在重写网址

http://domain.com/profile/?u=10000017564881
Run Code Online (Sandbox Code Playgroud)

对此

http://domain.com/profile/10000017564881
Run Code Online (Sandbox Code Playgroud)

以下重写

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)\/?$ index.php?u=$1 [L]
Run Code Online (Sandbox Code Playgroud)

但是我想优化seo a litte并选择:

http://domain.com/profile/10000017564881/Anything-I-want-here
Run Code Online (Sandbox Code Playgroud)

显然,/ Anything-I-want-here只是忽略了...

有什么想法吗?非常感激

zil*_*n01 6

只需$从正则表达式中删除,ID号后面的任何内容都将被忽略,URL将被正确重写.

RewriteRule ^(.*?)\/? index.php?u=$1 [L]

# the following will work the same (as far as I can tell), and
# it's a lot more obvious at first glance what it does, which is
# match everything until the first slash
RewriteRule ^([^/]+)     ...
Run Code Online (Sandbox Code Playgroud)

当我做这样的事情时,我喜欢在代码中验证URL,如果"Anything-I-want-here"与数据不匹配,则301重定向.