Options +Indexes
# or #
IndexIgnore *
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?u=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?u=$1
我想在重写规则中添加点,以便它们之间带有点的用户名像 rahul.kapoor 一样工作,但 rahulkapoor 工作,请帮忙。
Options +Indexes
# or #
IndexIgnore *
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?u=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?u=$1
我想在重写规则中添加点,以便它们之间带有点的用户名像 rahul.kapoor 一样工作,但 rahulkapoor 工作,请帮忙。
尽管点通常在正则表达式中具有特殊含义,但在字符类中使用时它不是元字符,因此在您的情况下,您可以使用:
RewriteRule ^([a-zA-Z0-9_.-]+)/?$ profile.php?u=$1
请注意,我已将末尾的正斜杠设为可选,因此您只需使用一行而不是两行。
编辑:您还可以使用元字符一词来进一步简化它:
RewriteRule ^([\w.-]+)/?$ profile.php?u=$1
.htaccess
对于文件中使用的正则表达式,点字符具有特殊含义。它的意思是“(几乎)任何字符”。
如果您想使用“真正的”点字符,则必须使用反斜杠对其进行转义:
RewriteRule ^([a-zA-Z0-9_-]+\.?[a-zA-Z0-9_-]*)/$ profile.php?u=$1
我在这里所做的是更改您的正则表达式规则以匹配:
[a-zA-Z0-9_-]+
\.?
[a-zA-Z0-9_-]*