Regex 重写.htaccess上包含点字符的模式匹配的规则无效
我正在使用htaccess创建一个网站,但由于浏览器中出现错误,出现以下情况之一,该网站无法正常工作:Regex 重写.htaccess上包含点字符的模式匹配的规则无效,regex,.htaccess,mod-rewrite,Regex,.htaccess,Mod Rewrite,我正在使用htaccess创建一个网站,但由于浏览器中出现错误,出现以下情况之一,该网站无法正常工作: The page isn't redirecting properly 重写规则行为: RewriteRule ^([a-zA-Z0-9-_\.]+)$ index.php?user=$1 [NC,L] 我用它来获取get变量中的用户配置文件,如果像这样删除点字符,问题似乎就解决了: RewriteRule ^([a-zA-Z0-9-_]+)$ index.php?
The page isn't redirecting properly
重写规则行为:
RewriteRule ^([a-zA-Z0-9-_\.]+)$ index.php?user=$1 [NC,L]
我用它来获取get变量中的用户配置文件,如果像这样删除点字符,问题似乎就解决了:
RewriteRule ^([a-zA-Z0-9-_]+)$ index.php?user=$1 [NC,L]
但是我需要用户名格式来包含点字符
它应该如何工作的示例:
http://www.example.dev/john.88 >>> index.php?user=john.88
http://www.example.dev/johnsmith >>> index.php?user=johnsmith
http://www.example.dev/john_smith >>> index.php?user=john_smith
提前感谢。在模式中使用点确实会导致无限循环,因为重写的URI
/index.php
也与您的模式匹配
要解决此问题,您需要重写cond:
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w.-]+)/?$ index.php?user=$1 [QSA,L]
还请注意,可以使用
\w
缩短正则表达式模式,这相当于[a-zA-Z0-9.
新的URL也与表达式匹配,因此得到一个无限循环。它与点无关,您也可以删除字母e
:)我使用的重写规则用作最后一条规则,删除点可以正常工作,而不会导致任何循环。删除字母e是什么意思?index.php?user=$1
匹配^([a-zA-Z0-9-\]+)$
。如果删除点,则会得到一个表达式,它不再匹配(^([a-zA-Z0-9-\+)$
),但如果删除index.php?user=$1
(例如,^([a-df-zA-Z0-9-\.]+)$
)中的任何其他字母,也会出现同样的情况。