URL重写奇怪的错误

时间:2016-05-26 14:42:09

标签: .htaccess mod-rewrite

这些是我遇到问题的重写规则。

# Pretty URLs
RewriteRule ^article/(.+)$ /article?query=$1

# Resolve .php file for extensionless php urls
RewriteRule ^([/A-Za-z0-9-]+)$ /$1.php

这是我尝试构建的网址

http://www.example.com/article/392-demolition

当我调试时,我发现rewriterules实际上是将请求发送到

http://www.example.com/article.php?query=392-demolition.php

如果我从第二个规则中删除斜杠或将[R]添加到第一个规则,一切正常。谁能解释为什么或这里的错误是什么?

由于

2 个答案:

答案 0 :(得分:0)

使用简单的重写:

RewriteRule ^article/([^/]*)$ /article.php?query=$1 [L]

答案 1 :(得分:0)

您可以使用:

# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# Pretty URLs
RewriteRule ^article/(.+)$ /article?query=$1 [NC,L]

# Resolve .php file for extensionless php urls (with test)
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+)/?$ /$1.php [L]