Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 添加mod_重写规则会导致整个目录的服务器错误_Php_Regex_Apache_.htaccess_Mod Rewrite - Fatal编程技术网

Php 添加mod_重写规则会导致整个目录的服务器错误

Php 添加mod_重写规则会导致整个目录的服务器错误,php,regex,apache,.htaccess,mod-rewrite,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

我有一个测试文件夹(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]
对于my.htaccess文件,test目录中的所有内容都会出现
500内部服务器错误
。所有其他规则都像它们应该的那样工作

我想做的是让
example.com/test/$1
转到
example.com/test/projects.php?project=$1
,除了“projects”、“about”和“sitemap.xml”之外。每个项目中都有编号的帖子,在url中有可选的标题部分

服务器正在运行apache 2.2.15


如果您需要更多信息或想让我测试某些东西,请告诉我。

试试这个。htaccess在
/test/
文件夹中:

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]

您现在制定的规则应产生以下结果:

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

假设您在访问或错误日志中看不到任何有用的信息,则考虑向主机添加条目。

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

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

更清晰的规则集。你能解释一下根本问题是什么吗?太棒了,真管用!你知道为什么一条规则会扼杀它吗?你需要从重写中跳过文件和目录,否则
项目。php
将匹配
[^/]+
模式,它将导致无限循环,因此产生500个错误。啊,好的。为什么[L]规则不停止循环?不
L
不会停止所有规则,它只是中断当前的mod_rewrite循环。mod_rewrite将继续执行,直到任何规则都能够执行。感谢您的输入。这会很有帮助,因为我不知道如何记录错误。