WordPress Multisite, custom wp-content folder, and Request exceeded the limit of 10 internal redirects

时间:2016-02-12 20:07:17

标签: wordpress apache .htaccess redirect

I'm writing a WordPress plugin which uses the settings API from within a class. So the class calls register_settings(), add_settings_field(), and add_settings_section() from within the class which is working well except when I try to submit the form a get a server error with the following error in my logs:

AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.

My WordPress install is a bit weird. It's based on Mark Jaquith's WordPress Skeleton repo and I have moved my wp-content folder, my wp-config.php file outside WordPress.

I am also running WordPress through Vagrant so it might be a server config issue. However, I just tried to use my plugin in VVV using a standard WordPress install and its working perfectly.

My WordPress directory looks like this (composer install WP to the app dir):

index.php
wp-config.php
wp-content/
app/

I have added my rewrite logs in a paste bin: http://pastebin.com/QKCFjULZ

My .htaccess file looks like this:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

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


RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]

RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-.*) /app/$2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ /app/$2 [L]


RewriteRule . /index.php [L]

Options -indexes
</IfModule>
# END WordPress

1 个答案:

答案 0 :(得分:1)

由于这些规则,您有重定向循环

RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-.*) /app/$2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ /app/$2 [L]

实际上,您必须记住,即使重写后您的规则也会被评估。因此,如果您有/something/wp-xxx/yyy/zzz,则会在内部将其重写为/app/wp-xxx/yyy/zzz。但现在,正如您所看到的,您的规则将再次与新的uri /app/wp-xxx/yyy/zzz匹配。

证明,查看您的日志

applying pattern '^([_0-9a-zA-Z-]+/)?(wp-.*)' to uri 'wp-admin/network/options.php',
rewrite 'wp-admin/network/options.php' -> '/app/wp-admin/network/options.php',

然后你有

applying pattern '^([_0-9a-zA-Z-]+/)?(wp-.*)' to uri 'app/wp-admin/network/options.php',
rewrite 'app/wp-admin/network/options.php' -> '/app/wp-admin/network/options.php',

它一直持续......直到10次重定向的限制

要避免此行为,您需要对规则添加一些限制,以确保在不需要时不会对其进行评估。一个简单的解决方法是使用负前瞻模式来确保当前请求在重写之前/app/开头:

RewriteRule ^((?!app/)[^/]+/)?(wp-.+)$ /app/$2 [L]  

最后,你的htaccess看起来像这样

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# Rule 1
RewriteRule ^([^/]+/)?files/(.+)$ wp-includes/ms-files.php?file=$2 [L]

# Rule 2
RewriteRule ^((?!app/)[^/]+/)?(wp-.+)$ /app/$2 [L,QSA]

# Rule 3
RewriteRule ^((?!app/)[^/]+/)?(.+\.php)$ /app/$2 [L,QSA] 

RewriteRule ^ index.php [L]

Options -Indexes
</IfModule>
# END WordPress

提醒:3条规则仅在不存在的文件/文件夹上执行。

示例:

  • http://domain.tld/files/somethinghttp://domain.tld/xxx/files/something将在内部重写 感谢规则1
  • /wp-includes/ms-files.php?file=something 由于规则2,
  • http://domain.tld/wp-something/somethingelsehttp://domain.tld/xxx/wp-something/somethingelse(其中xxx可以是除app之外的任何内容)将在内部重写为/app/wp-something/somethingelse
  • 由于规则3,
  • http://domain.tld/file.phphttp://domain.tld/xxx/file.php(其中xxx可以是除app之外的任何内容)将在内部重写为/app/file.php