And*_*ski 0 php .htaccess mod-rewrite
我从php开始,我想在网址中使用点(。)。例如,使用用户名test.test1的用户应指出具有该用户名的用户的个人资料,但我收到一条错误消息,提示未找到对象。但是,如果我使用字母,数字,破折号或下划线,则效果很好。

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?profile_username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?profile_username=$1
Run Code Online (Sandbox Code Playgroud)
这是我所有的.htaccess代码。
您的正则表达式与点不匹配。
方括号之间的字符序列表示“仅将这些字符匹配一次”,而括号外的字符+表示“匹配多次”。因此,在请求的URL中仅匹配括号之间定义的字符,该URL不包含点。您应该在字符序列中添加“点字符”。
所以你的正则表达式^([a-zA-Z0-9_-]+)$应该变成^([a-zA-Z0-9_-.]+)$
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-.]+)$ profile.php?profile_username=$1
RewriteRule ^([a-zA-Z0-9_-.]+)/$ profile.php?profile_username=$1
Run Code Online (Sandbox Code Playgroud)
另外,为了不匹配文件或目录中实际存在的路径,请在几乎与“所有内容”都匹配的规则(包括重写目标(profile.php))之前使用以下条件:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Run Code Online (Sandbox Code Playgroud)
最终的.htaccess文件如下所示:
RewriteEngine On
# this rule should not match existing files and directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-.]+)$ profile.php?profile_username=$1
# this rule should not match existing directories
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-.]+)/$ profile.php?profile_username=$1
Run Code Online (Sandbox Code Playgroud)
PS:我建议您阅读一些有关正则表达式的文章,并使用“ Regex Tester”来学习正则表达式。例如,此站点是一个很好的起点,而这是一个很好的在线测试人员。