【问题标题】:Nested RewriteRules on Apache 2.4 causing 500 errorsApache 2.4 上的嵌套 RewriteRules 导致 500 错误
【发布时间】:2016-08-27 16:31:48
【问题描述】:

我有一个在 cPanel 上运行的站点,其中 wordpress 安装在根目录中。我还编写了一个自定义应用程序,它位于名为“gallery”的子文件夹中。在画廊子文件夹中有一个“管理员”文件夹。 Gallery 的根文件夹(无重写)控制信息的显示,其中“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 报告的错误是:

[Thu Aug 25 21:33:21.323468 2016] [core:error] [pid 15805] [client XX.XX.XX.XX:XXXXX] AH00124:由于可能的配置错误,请求超出了 10 个内部重定向的限制.如有必要,使用“LimitInternalRecursion”增加限制。使用“LogLevel debug”获取回溯。

如果我从gallery/admin/.htaccess 文件中注释掉RewriteRule 行的任何组合,则消息是相同的。

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

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

【问题讨论】:

  • 经验法则:当您获得 500 分时,您的第一站是检查 apache 错误日志以获取详细信息。其他任何事情都是没有意义的——除非你知道问题出在哪里,否则在黑暗中随意刺伤是没有意义的。
  • RewriteEngine On 行下方添加RewriteRule ^controller\.php - [L,NC] 规则
  • controller.php 存在于何处,您用于测试的确切 URL 是什么?
  • @anubhava - controller.php 位于/gallery/admin/ 并处理不存在任何过滤类型的文件的所有页面请求(物理上找不到图像),并充当控制器处理请求的每个页面的 MVC 框架。我赞成的您的解决方案对我有用,尽管仍然不清楚为什么我认为^ 后跟 ` - [L]` 表示排除,并且首先似乎将其完全排除在处理之外。但它有效。你能解释一下为什么吗?
  • 好的,我在下面添加了一个带有修改版/gallery/admin/.htaccess 的答案。我在每条规则上方添加了 cmets 以使其清楚。希望对您有所帮助。

标签: php wordpress apache .htaccess mod-rewrite


【解决方案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]

【讨论】:

    猜你喜欢
    • 2016-08-04
    • 2021-10-04
    • 2017-07-15
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-22
    相关资源
    最近更新 更多