Apache 2.4上的嵌套RewriteRules导致500个错误

时间:2016-08-25 22:03:23

标签: php wordpress apache .htaccess mod-rewrite

我在cPanel上运行了一个站点,其中wordpress安装在root中。我还有一个自定义的应用程序,它位于一个名为“gallery”的子文件夹中。在gallery子文件夹中是一个'admin'文件夹。库的根文件夹(无重写)控制信息的显示,其中“gallery / admin”的根授予用户对应用程序的管理访问权限。路径结构被重定向和处理(自定义MVC框架),主要由以下.htaccess指令指导:

要求全部授予

RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(jpg|png|bmp|gif|css|js|map|ttf|woff|woff2|svg|eot)$
RewriteRule ^(.*)$ controller.php?page=$1 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ controller.php?page=error&rewritten=1&missing=$1 [NC,L,QSA]

wordpress的.htaccess是为压缩,过期/缓存不同类型而定制的,除了处理传统的wordpress内容之外,如下(完全粘贴):

Options -Indexes
# BEGIN WordPress

# Expires Caching (Leverage Browser Caching)
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 2 week"
ExpiresByType image/jpeg "access 2 week"
ExpiresByType image/gif "access 2 week"
ExpiresByType image/png "access 2 week"
ExpiresByType text/css "access 2 week"
ExpiresByType application/pdf "access 2 week"
ExpiresByType text/x-javascript "access 2 week"
ExpiresByType application/x-shockwave-flash "access 2 week"
ExpiresByType image/x-icon "access 2 week"
ExpiresDefault "access 2 week"
</IfModule>

# Enable GZIP Compression (compress text, html, javascript, css, xml and so on) ##
<ifModule mod_gzip.c>
  mod_gzip_on Yes
  mod_gzip_dechunk Yes
  mod_gzip_item_include file \.(html?|txt|css|js|pl)$
  mod_gzip_item_include handler ^cgi-script$
  mod_gzip_item_include mime ^text/.*
  mod_gzip_item_include mime ^application/x-javascript.*
  mod_gzip_item_exclude mime ^image/.*
  mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
#AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
</IfModule>

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.org
RewriteRule (.*) http://www.example.org/$1 [R=301,L]
#RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^sitemap.xml /sitemap.php?p=xml [L]
RewriteRule ^sitemap.xsl /sitemap.php?p=xsl [L]
RewriteRule ^sitemap.css /sitemap.php?p=css [L]
RewriteRule ^sitemap.js /sitemap.php?p=js [L]
RewriteRule ^robots.txt /sitemap.php?p=txt [L]
RewriteRule ^gallery - [L]
RewriteRule ^controller\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

双重检查RewriteCond %{REQUEST_FILENAME} !-f背后的原因是因为我尝试仅处理文件不存在的特定类型的文件时遇到的另一个问题。 (当jpg / etc丢失时,找不到404页面。)

正如您所看到的,controller.php完成了所有用于身份验证等工作的繁琐工作。如果我注释掉包含^(.*)$ controller.php?的任何行,除非我直接调用控制器,否则不会出现500错误并让它根据应用程序根路径重定向。

这在我的Debian 8和Ubuntu 11机器以及Windows 10上完美运行。管理员基于开源项目otp-thing并且运行良好。当全能wordpress在网站的根目录时,处理重定向时遇到一些麻烦。

似乎没有任何代码问题(如果是这样,它们会在Debian 8,Ubuntu 11,尤其是Windows 10的其他测试中出现。)

在此cPanel版本上运行的服务器软件如下:

Server version: Apache/2.4.18 (Unix)
Server built:   Mar  1 2016 10:43:46
Cpanel::Easy::Apache v3.32.10 rev9999
Server's Module Magic Number: 20120211:52
Server loaded:  APR 1.5.2, APR-UTIL 1.5.4
Compiled using: APR 1.5.2, APR-UTIL 1.5.4
Architecture:   64-bit
Server MPM:     prefork
  threaded:     no
    forked:     yes (variable process count)
Server compiled with....
 -D APR_HAS_SENDFILE
 -D APR_HAS_MMAP
 -D APR_HAVE_IPV6 (IPv4-mapped addresses disabled)
 -D APR_USE_SYSVSEM_SERIALIZE
 -D APR_USE_PTHREAD_SERIALIZE
 -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
 -D APR_HAS_OTHER_CHILD
 -D AP_HAVE_RELIABLE_PIPED_LOGS
 -D DYNAMIC_MODULE_LIMIT=256
 -D HTTPD_ROOT="/usr/local/apache"
 -D SUEXEC_BIN="/usr/local/apache/bin/suexec"
 -D DEFAULT_PIDLOG="logs/httpd.pid"
 -D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
 -D DEFAULT_ERRORLOG="logs/error_log"
 -D AP_TYPES_CONFIG_FILE="conf/mime.types"
 -D SERVER_CONFIG_FILE="conf/httpd.conf"

和PHP

PHP 5.6.18 (cli) (built: Mar  1 2016 10:47:52) 
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
    with the ionCube PHP Loader v4.7.5, Copyright (c) 2002-2014, by ionCube Ltd., and
    with Zend Guard Loader v3.3, Copyright (c) 1998-2014, by Zend Technologies

另外,如果您查看otp-thing的项目源,您会注意到一些嵌套目录故意阻止公共访问(app / cache,app / templates_c等)

Apache在/usr/local/apache/error_log中报告的错误是:

  

[8月25日星期二21:33:21.323468 2016] [核心:错误] [pid 15805] [客户端XX.XX.XX.XX:XXXXX] AH00124:由于可能的配置错误,请求超出了10次内部重定向的限制。如有必要,使用'LimitInternalRecursion'增加限制。使用“LogLevel debug”获取回溯。

如果我注释掉RewriteRule文件中gallery/admin/.htaccess行的任意组合,则该邮件是相同的。

我尝试在'controller.php'之前添加路径,但是这会加剧问题,但总的来说,这里的问题没有改变。

我可以做些什么来确保所有这些东西都有效,或者甚至作为一种解决方法,如何通过合并将如何将otp-thing的htaccess规则移动到wordpress htaccess文件中(如何调整指令) )。

1 个答案:

答案 0 :(得分:1)

这应该适用于/gallery/admin/.htaccess

RewriteEngine On
RewrtieBase /gallery/admin/

# skip any rule below this for controller.php or for valid files / directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_URI} /controller\.php$ [NC]
RewriteRule ^ - [L]

# rewrite all except certain static resource extensions to controller.php
RewriteCond %{REQUEST_URI} !\.(jpe?g|png|bmp|gif|css|js|map|ttf|woff|woff2|svg|eot)$
RewriteRule ^.*$ controller.php?page=$0 [L,QSA]

# rewrite not found static resource extensions
RewriteRule ^.*$ controller.php?page=error&rewritten=1&missing=$0 [L,QSA]