.htaccess出现URL问题


.htaccess pretty URL trouble

我一直在开发一个.htaccess文件来创建一些漂亮的URL。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^([a-z0-9]+)/?$ index.php?key=$1 [NC,L]
RewriteRule ^([a-z0-9]+)/([a-z0-9]+)/?$ index.php?key=$1 [NC,L]
RewriteRule ^([^'.]+)$ $1.php [NC,L]

这是我正在使用的代码。现在,如果我键入www.example.com/test,它的行为就像www.example.com/index.php?key=test,但问题是当我输入www.example.com/test-2时,它的行为与www.example.com/index.php不同?key=test-2更倾向于搜索test-2.php文件。我该怎么解决这个问题?我希望它也能用"-"重定向键。

提前感谢

答案是在.htaccess文件中添加一个"-"字符,如下所示:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^([a-z0-9-]+)/?$ index.php?key=$1 [NC,L]
RewriteRule ^([a-z0-9-]+)/([a-z0-9]+)/?$ index.php?key=$1 [NC,L]
RewriteRule ^([^'.]+)$ $1.php [NC,L]

希望它也能帮助你:)。

感谢Mario和Eric