添加mod_rewrite规则会导致整个目录出现服务器错误

时间:2015-07-24 19:20:17

标签: php regex apache .htaccess mod-rewrite

我有一个测试文件夹(example.com/test),它有自己的htaccess文件。它看起来像这样:

RewriteEngine On

RewriteRule ^(projects|about)/?$ /test/$1.php [L]
RewriteRule ^sitemap\.xml$ /test/xmlsitemap.php [L]

RewriteRule ^([^/]+)/?$ /test/projects.php?project=$1 [L] #this line causes trouble
RewriteRule ^([^/]+)/([0-9]+)/?$ /test/posts.php?&project=$1&post=$2 [L]
RewriteRule ^([^/]+)/([0-9]+)/([^/]+)/?$ /test/posts.php?project=$1&post=$2&title=$3 [L]

当我添加此行时: RewriteRule ^([^/]+)/?$ /test/projects.php?project=$1 [L] 在我的.htaccess文件中,测试目录中的所有内容都为500 Internal Server Error。所有其他规则的工作方式都应该如此。

我尝试做的是让example.com/test/$1转到example.com/test/projects.php?project=$1,除了"项目," "约,"和" sitemap.xml。"每个项目中还有编号的帖子,其中包含网址的可选标题部分。

服务器正在运行apache 2.2.15。

如果您需要更多信息或希望我测试某些内容,请与我们联系。

2 个答案:

答案 0 :(得分:2)

/test/文件夹中尝试此.htaccess:

RewriteEngine On
RewriteBase /test/

RewriteRule ^(projects|about)/?$ $1.php [L]
RewriteRule ^sitemap\.xml$ xmlsitemap.php [L]

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

RewriteRule ^([^/]+)/?$ projects.php?project=$1 [L,QSA]
RewriteRule ^([^/]+)/([0-9]+)/?$ posts.php?&project=$1&post=$2 [L,QSA]
RewriteRule ^([^/]+)/([0-9]+)/([^/]+)/?$ posts.php?project=$1&post=$2&title=$3 [L,QSA]

答案 1 :(得分:0)

您现有的规则应该产生以下结果:

http://example.com/test/1 -> http://example.com/test/posts.php?&project=test&post=1 

假设您在访问或错误日志中没有看到任何有用的内容,请考虑向主机添加rewrite log条目。

RewriteLog /var/log/httpd/rewrite.log
RewriteLogLevel 9

这可能有助于检测可能导致内部服务器错误的重定向循环。