用mod_rewrite重写URL

时间:2010-06-09 21:17:25

标签: .htaccess mod-rewrite url-rewriting

我目前正在重写

中的网址
http://domain.com/profile/?u=10000017564881

这个到

http://domain.com/profile/10000017564881

以下重写

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

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

http://domain.com/profile/10000017564881/Anything-I-want-here

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

有什么想法吗? 非常感谢

1 个答案:

答案 0 :(得分: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 ^([^/]+)     ...

当我做这样的事情时,我想验证代码中的URL,如果“Anything-I-want-here”与数据不匹配,则301重定向。