Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.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
Apache htaccess重定向卡在无限递归中_Apache_.htaccess_Mod Rewrite - Fatal编程技术网

Apache htaccess重定向卡在无限递归中

Apache htaccess重定向卡在无限递归中,apache,.htaccess,mod-rewrite,Apache,.htaccess,Mod Rewrite,我似乎有一个奇怪的问题,只发生在一台机器上,而不是另一台机器上,这台机器上的apache2版本是2.4.23(不工作的版本),而在工作的机器上,我有apache2.4.10版本 如果文件存在,我的重写代码段会在内部将URL重定向到没有扩展名的.html,如下所示 RewriteCond %{REQUEST_FILENAME}.html -f RewriteRule ^(.*?)/?$ $1.html [L,QSA] 我删除了这一部分,重定向循环停止,但显然我的无扩展URL不再被重定向 我已经将

我似乎有一个奇怪的问题,只发生在一台机器上,而不是另一台机器上,这台机器上的apache2版本是2.4.23(不工作的版本),而在工作的机器上,我有apache2.4.10版本

如果文件存在,我的重写代码段会在内部将URL重定向到没有扩展名的.html,如下所示

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*?)/?$ $1.html [L,QSA]
我删除了这一部分,重定向循环停止,但显然我的无扩展URL不再被重定向

我已经将它发展到这个地步,我认为这肯定会停止重定向问题,但仍然会永远循环

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_FILENAME} !\.html$
RewriteRule ^(.*?)/?(?!\.html/)$ $1.html [L,QSA]
在所讨论的apache服务器上启用调试日志级别会产生大量与此类似的行

[Wed May 30 11:49:25.669176 2018] [rewrite:trace3] [pid 4292] mod_rewrite.c(477): [client xx.xx.xxx.xxx:63613] xx.xx.xxx.xxx - - [domain.com/sid#7f30066c1328][rid#7f2fff7d5718/initial/redir#9] [perdir /domains/domain.com/public_html/] applying pattern '^(.*)$' to uri '2012/06/some-blog-post/privacy.html.html.html.html.html.html.html.html.html'
[Wed May 30 11:49:25.669200 2018] [rewrite:trace3] [pid 4292] mod_rewrite.c(477): [client xx.xx.xxx.xxx:63613] xx.xx.xxx.xxx - - [domain.com/sid#7f30066c1328][rid#7f2fff7d5718/initial/redir#9] [perdir /domains/domain.com/public_html/] add path info postfix: /domains/domain.com/public_html/2012/06/some-blog-post -> /domains/domain.com/public_html/2012/06/some-blog-post/privacy.html.html.html.html.html.html.html.html.html
[Wed May 30 11:49:25.669215 2018] [rewrite:trace3] [pid 4292] mod_rewrite.c(477): [client xx.xx.xxx.xxx:63613] xx.xx.xxx.xxx - - [domain.com/sid#7f30066c1328][rid#7f2fff7d5718/initial/redir#9] [perdir /domains/domain.com/public_html/] strip per-dir prefix: /domains/domain.com/public_html/2012/06/some-blog-post/privacy.html.html.html.html.html.html.html.html.html -> 2012/06/some-blog-post/privacy.html.html.html.html.html.html.html.html.html
恐怕我对htaccess语法不是非常精通,所以我可能遗漏了一些明显的东西


这是整个htaccess文件(抱歉,有点长)



启用调试日志级别后,我抓取了更多(一吨)的apache2日志并将其放在这里,将
LogLevel alert rewrite:trace3
添加到我的apache2.conf文件中在您的站点根目录中只有这段代码。htaccess并在启用
RewriteLog
的情况下重新测试:

Options +FollowSymLinks -Indexes +MultiViews

# commenting out for now
#ErrorDocument 404   /404.html
#ErrorDocument 500   /505.html

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]

RewriteCond %{ENV:REDIRECT_STATUS} ^$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule ^img/([a-f0-9]{64})\.gif$ scripts/trackemail.php?id=$1 [QSA,NC,L]

启用
MultiViews
将使Apace能够正确匹配
.php
.html
文件。参见

我在禁用多视图选项时偶然发现的另一个答案似乎是将
%{REQUEST\u FILENAME}
的所有实例替换为
%{DOCUMENT\u ROOT}%{REQUEST\u URI}

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule .* - [L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_FILENAME} !\.php$
RewriteRule ^(.*?)/?$ $1.php [L,QSA]

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_FILENAME} !\.html$
RewriteRule ^(.*?)/?(?!\.html/)$ $1.html [L,QSA]
变成

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -l
RewriteRule ^ - [L]

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]

不知道为什么这会修复它,但它显然表现得更符合您的预期,不确定这是否是一个bug。尽管如此,另一个答案的作者还是为我提供了巨大的帮助。

@anubhava确实如此,但请注意,删除相关代码会阻止重定向loop@anubhava哦,这应该是我列出的第一件事“/2012/06/一些博客文章/隐私”@anubhava还应该注意的是,一个文件并没有(甚至不是html文件)存在于该路径,但有一个“/privacy.html”根目录下的文件可能会引起一些奇怪的问题issues@anubhava我曾考虑过浏览器缓存,但我在apache2日志中发现了错误,所以这不应该是问题所在case@anubhava是的,同样的问题,在incog Chrome和private Firefox中尝试过,很抱歉我做了一次手术,耽误了我一段时间,我正在抓紧时间这是一个高级别的调试,现在是的,只是,复制和粘贴,没有别的。这有点长,但我已经粘贴了大量的输出。我已经为这个问题为你的答案添加了一笔奖金,因为你帮了大忙,所以我会奖励它。让我说:)我找到了另一个解决方案,为了防止你的好奇,我添加了第二个答案,直到我选择的那一个,你都觉得你的一个Apache配置很可能有点奇怪,它使
%{REQUEST\u FILENAME}
不能扩展到
.php
.html
文件的完整路径。但是很好的发现++也许,我注意到,即使我用随机字母替换了“隐私”,而这些字母肯定不存在于任何地方的文件中,我仍然会遇到同样的问题,很奇怪,但不管怎样,哈哈
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule .* - [L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_FILENAME} !\.php$
RewriteRule ^(.*?)/?$ $1.php [L,QSA]

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_FILENAME} !\.html$
RewriteRule ^(.*?)/?(?!\.html/)$ $1.html [L,QSA]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -l
RewriteRule ^ - [L]

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]