【问题标题】:Rewrite rule causes 'Request exceeded the limit of 10 internal redirects'重写规则导致“请求超出 10 个内部重定向的限制”
【发布时间】:2014-06-28 02:23:34
【问题描述】:

为什么.htaccess 文件中的上述行导致:

RewriteRule ^([A-Za-z0-9\_\-]+)\/(.*)$ engine/handlers/page_handler.php?handler=$1&page=$2 [QSA,L]

error_log 中的错误如下:

请求超出了 10 次内部重定向的限制,因为可能 配置错误。使用“LimitInternalRecursion”来增加 必要时限制。使用“LogLevel debug”获取回溯。

我在 Stack Overflow 上找到了很多帖子,但没有一个解决方案适合我。我有一个带有 cPanel 的 CentOS 服务器。如果没有 cPanel,这不会在我的服务器上发生。
这是我的 .htaccess 文件:

<Files "htaccess_dist">
    order allow,deny
    deny from all
</Files>
Options -Indexes
Options +FollowSymLinks
DirectoryIndex index.php
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 1 year"
</IfModule>
<FilesMatch "\.(jpg|jpeg|gif|png|mp3|flv|mov|avi|3pg|html|htm|swf|js|css|ico)$">
    FileETag MTime Size
</FilesMatch>
<IfModule mod_php5.c>   
    php_value memory_limit 64M  
    php_value register_globals 0    
    php_value post_max_size 8388608 
    php_value upload_max_filesize 5242880   
    php_value display_errors 0
</IfModule>
<IfModule mod_gzip.c>
    mod_gzip_on yes
    mod_gzip_dechunk yes
    mod_gzip_keep_workfiles No
    mod_gzip_minimum_file_size 1000
    mod_gzip_maximum_file_size 1000000
    mod_gzip_maximum_inmem_size 1000000
    mod_gzip_item_include mime ^text/.*
    mod_gzip_item_include mime ^application/javascript$
    mod_gzip_item_include mime ^application/x-javascript$
    # Exclude old browsers and images since IE has trouble with this
    mod_gzip_item_exclude reqheader "User-Agent: .*Mozilla/4\..*\["
    mod_gzip_item_exclude mime ^image/.*
</IfModule>
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/x-javascript
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4\.[0678] no-gzip
    BrowserMatch \bMSIE !no-gzip

<IfModule mod_headers.c>
    Header append Vary User-Agent env=!dont-vary
</IfModule> 
    SetEnvIfNoCase Request_URI action\/* no-gzip dont-vary
    SetEnvIfNoCase Request_URI actions\/* no-gzip dont-vary

</IfModule>
<IfModule mod_rewrite.c>

RewriteEngine on
RewriteBase /
RewriteRule ^pg\/([A-Za-z0-9\_\-]+)$ engine/handlers/page_handler.php?handler=$1&%{QUERY_STRING} [L]
RewriteRule ^pg\/([A-Za-z0-9\_\-]+)\/(.*)$ engine/handlers/page_handler.php?handler=$1&page=$2&%{QUERY_STRING} [L]
RewriteRule ^tag\/(.+)\/?$ engine/handlers/page_handler.php?handler=search&page=$1 [L]


RewriteRule ^action\/([A-Za-z0-9\_\-\/]+)$ engine/handlers/action_handler.php?action=$1&%{QUERY_STRING} [L]

RewriteRule ^cache\/(.*)$ engine/handlers/cache_handler.php?request=$1&%{QUERY_STRING} [L]

RewriteRule ^services\/api\/([A-Za-z0-9\_\-]+)\/(.*)$ engine/handlers/service_handler.php?handler=$1&request=$2&%{QUERY_STRING} [L]

RewriteRule ^export\/([A-Za-z]+)\/([0-9]+)\/?$ engine/handlers/export_handler.php?view=$1&guid=$2 [L]
RewriteRule ^export\/([A-Za-z]+)\/([0-9]+)\/([A-Za-z]+)\/([A-Za-z0-9\_]+)\/$ engine/handlers/export_handler.php?view=$1&guid=$2&type=$3&idname=$4 [L]

RewriteRule xml-rpc.php engine/handlers/xml-rpc_handler.php [L]
RewriteRule mt/mt-xmlrpc.cgi engine/handlers/xml-rpc_handler.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9\_\-]+)$ engine/handlers/page_handler.php?handler=$1 [QSA,L]


RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9\_\-]+)\/(.*)$ engine/handlers/page_handler.php?handler=$1&page=$2 [QSA,L]
RewriteRule . /~user/sub/
</IfModule>

当我在规则中添加以下内容时,内部重定向停止并且错误消失但 css 和图像不加载:

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^.* - [L]

这是我的 httpd.conf 文件:

Include "/usr/local/apache/conf/includes/pre_main_global.conf"
Include "/usr/local/apache/conf/includes/pre_main_2.conf"


LoadModule bwlimited_module modules/mod_bwlimited.so


Include "/usr/local/apache/conf/php.conf"
Include "/usr/local/apache/conf/includes/errordocument.conf"

ErrorLog "logs/error_log"
DefaultType text/plain
ScriptAliasMatch ^/?controlpanel/?$ /usr/local/cpanel/cgi-sys/redirect.cgi
ScriptAliasMatch ^/?cpanel/?$ /usr/local/cpanel/cgi-sys/redirect.cgi
ScriptAliasMatch ^/?kpanel/?$ /usr/local/cpanel/cgi-sys/redirect.cgi
ScriptAliasMatch ^/?securecontrolpanel/?$ /usr/local/cpanel/cgi-sys/sredirect.cgi
ScriptAliasMatch ^/?securecpanel/?$ /usr/local/cpanel/cgi-sys/sredirect.cgi
ScriptAliasMatch ^/?securewhm/?$ /usr/local/cpanel/cgi-sys/swhmredirect.cgi
ScriptAliasMatch ^/?webmail/?$ /usr/local/cpanel/cgi-sys/wredirect.cgi
ScriptAliasMatch ^/?whm/?$ /usr/local/cpanel/cgi-sys/whmredirect.cgi

Alias /bandwidth /usr/local/bandmin/htdocs/
Alias /img-sys /usr/local/cpanel/img-sys/
Alias /java-sys /usr/local/cpanel/java-sys/
Alias /mailman/archives /usr/local/cpanel/3rdparty/mailman/archives/public/
Alias /pipermail /usr/local/cpanel/3rdparty/mailman/archives/public/
Alias /sys_cpanel /usr/local/cpanel/sys_cpanel/

ScriptAlias /cgi-sys /usr/local/cpanel/cgi-sys/
ScriptAlias /mailman /usr/local/cpanel/3rdparty/mailman/cgi-bin/
ScriptAlias /scgi-bin /usr/local/cpanel/cgi-sys/scgiwrap

<Directory "/">
    Options All
    AllowOverride All
</Directory>

<Directory "/usr/local/apache/htdocs">
    Options Includes Indexes FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all    
</Directory>

<Files ~ "^error_log$">
    Order allow,deny
    Deny from all
    Satisfy All
</Files>

<FilesMatch "^\.ht">
    Order allow,deny
    Deny from all
    Satisfy All
</FilesMatch>

<IfModule log_config_module>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common

    CustomLog "logs/access_log" common

    <IfModule logio_module>
        LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio

    </IfModule>

</IfModule>

<IfModule alias_module>
    ScriptAlias /cgi-bin/ "/usr/local/apache/cgi-bin/"

</IfModule>

<Directory "/usr/local/apache/cgi-bin">
    AllowOverride None
    Options None
    Order allow,deny
    Allow from all
</Directory>

<IfModule mod_log_config.c>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
    LogFormat "%{Referer}i -> %U" referer
    LogFormat "%{User-agent}i" agent

    CustomLog logs/access_log common

</IfModule>

<IfModule mime_module>
    TypesConfig conf/mime.types
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz

</IfModule>
PidFile logs/httpd.pid
LockFile logs/accept.lock
Listen 0.0.0.0:80
Listen [::]:80
User nobody
Group nobody
ExtendedStatus On
ServerAdmin user@gmail.com
ServerName wdsl.saptik.com
LogLevel debug

# These can be set in WHM under 'Apache Global Configuration'
Timeout 300

ServerSignature On

<IfModule prefork.c>

</IfModule>
<IfModule itk.c>

</IfModule>

RewriteEngine on
RewriteMap LeechProtect prg:/usr/local/cpanel/bin/leechprotect
RewriteLock /usr/local/apache/logs/rewrite_lock

# Set UserDir directory for all virtual hosts, except..
UserDir public_html
# when the following two modules are loaded
<IfModule mod_ruid2.c>
UserDir disabled
</IfModule>
<IfModule itk.c>
UserDir disabled
</IfModule>

# DirectoryIndex is set via the WHM -> Service Configuration -> Apache Setup -> DirectoryIndex Priority
DirectoryIndex index.html.var index.htm index.html index.shtml index.xhtml index.wml index.perl index.pl index.plx index.ppl index.cgi index.jsp index.js index.jp index.php4 index.php3 index.php index.phtml default.htm default.html home.htm index.php5 Default.html Default.htm home.html

# SSLCipherSuite can be set in WHM under 'Apache Global Configuration'

SSLPassPhraseDialog  builtin
SSLSessionCache         dbm:/usr/local/apache/logs/ssl_scache
SSLSessionCacheTimeout  300
SSLMutex  file:/usr/local/apache/logs/ssl_mutex
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin

    Listen 0.0.0.0:443
    Listen [::]:443
    AddType application/x-x509-ca-cert .crt
    AddType application/x-pkcs7-crl .crl


AddHandler cgi-script .cgi .pl .plx .ppl .perl
AddHandler server-parsed .shtml
AddType text/html .shtml
AddType application/x-tar .tgz
AddType text/vnd.wap.wml .wml
AddType image/vnd.wap.wbmp .wbmp
AddType text/vnd.wap.wmlscript .wmls
AddType application/vnd.wap.wmlc .wmlc
AddType application/vnd.wap.wmlscriptc .wmlsc

<Location /whm-server-status>
    SetHandler server-status
    Order deny,allow
    Deny from all
    Allow from 127.0.0.1 ::1
</Location>



# SUEXEC is supported

Include "/usr/local/apache/conf/includes/pre_virtualhost_global.conf"
Include "/usr/local/apache/conf/includes/pre_virtualhost_2.conf"

# DO NOT EDIT. AUTOMATICALLY GENERATED.  IF YOU NEED TO MAKE A CHANGE PLEASE USE THE INCLUDE FILES.
NameVirtualHost 0.0.0.0:80
NameVirtualHost 127.0.0.1:80
NameVirtualHost *
# DO NOT EDIT. AUTOMATICALLY GENERATED.  IF YOU NEED TO MAKE A CHANGE PLEASE USE THE INCLUDE FILES.

<VirtualHost 0.0.0.0:80>
    ServerName 0.0.0.0
    DocumentRoot /usr/local/apache/htdocs
    ServerAdmin user@gmail.com
    <IfModule mod_suphp.c>
        suPHP_UserGroup nobody nobody
    </IfModule>
</VirtualHost>


# Default vhost for unbound IPs

<VirtualHost *>
    ServerName wdsl.adomain.com
    DocumentRoot /usr/local/apache/htdocs
    ServerAdmin user@gmail.com
    <IfModule mod_suphp.c>
        suPHP_UserGroup nobody nobody
    </IfModule>
</VirtualHost>

# DO NOT EDIT. AUTOMATICALLY GENERATED.  IF YOU NEED TO MAKE A CHANGE PLEASE USE THE INCLUDE FILES.


# SSL





# CPANEL/WHM/WEBMAIL/WEBDISK PROXY SUBDOMAINS

<VirtualHost 0.0.0.0:80 127.0.0.1:80>
    ServerName wdsl.adomain.com

    ServerAlias cpanel.* whm.* webmail.* webdisk.*

    DocumentRoot /usr/local/apache/htdocs
    ServerAdmin user@gmail.com
    <IfModule mod_suphp.c>
        suPHP_UserGroup nobody nobody
    </IfModule>
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^wdsl.adomain.com$
    RewriteCond %{HTTP_HOST} ^cpanel\.
    RewriteRule ^/(.*) http://127.0.0.1:2082/$1 [P]

    RewriteCond %{HTTP_HOST} !^wdsl.adomain.com$
    RewriteCond %{HTTP_HOST} ^webmail\.
    RewriteRule ^/(.*) http://127.0.0.1:2095/$1 [P]

    RewriteCond %{HTTP_HOST} !^wdsl.adomain.com$
    RewriteCond %{HTTP_HOST} ^whm\.
    RewriteRule ^/(.*) http://127.0.0.1:2086/$1 [P]

    RewriteCond %{HTTP_HOST} !^wdsl.adomain.com$
    RewriteCond %{HTTP_HOST} ^webdisk\.
    RewriteRule ^/(.*) http://127.0.0.1:2077/$1 [P]

    UseCanonicalName Off
</VirtualHost>






# CPANEL/WHM/WEBMAIL/WEBDISK PROXY SUBDOMAINS

<VirtualHost 0.0.0.0:443 127.0.0.1:443>
    ServerName wdsl.adomain.com

    ServerAlias cpanel.* whm.* webmail.* webdisk.*

    DocumentRoot /usr/local/apache/htdocs
    ServerAdmin user@gmail.com
    <IfModule mod_suphp.c>
        suPHP_UserGroup nobody nobody
    </IfModule>
    RewriteEngine On
    <IfModule mod_ssl.c>
        SSLEngine on
        SSLProxyEngine On
            SSLCertificateFile /var/cpanel/ssl/cpanel/cpanel.pem
        SSLCertificateKeyFile /var/cpanel/ssl/cpanel/cpanel.pem
        SSLCertificateChainFile /var/cpanel/ssl/cpanel/cpanel.pem

    </IfModule>
    RewriteCond %{HTTP_HOST} !^wdsl.adomain.com$
    RewriteCond %{HTTP_HOST} ^cpanel\.
    RewriteCond %{HTTPS} on
    RewriteRule ^/(.*) https://127.0.0.1:2083/$1 [P]

    RewriteCond %{HTTP_HOST} !^wdsl.adomain.com$
    RewriteCond %{HTTP_HOST} ^webmail\.
    RewriteCond %{HTTPS} on
    RewriteRule ^/(.*) https://127.0.0.1:2096/$1 [P]

    RewriteCond %{HTTP_HOST} !^wdsl.adomain.com$
    RewriteCond %{HTTP_HOST} ^whm\.
    RewriteCond %{HTTPS} on
    RewriteRule ^/(.*) https://127.0.0.1:2087/$1 [P]

    RewriteCond %{HTTP_HOST} !^wdsl.adomain.com$
    RewriteCond %{HTTP_HOST} ^webdisk\.
    RewriteCond %{HTTPS} on
    RewriteRule ^/(.*) https://127.0.0.1:2078/$1 [P]

    UseCanonicalName Off
</VirtualHost>

#JK

LoadModule jk_module modules/mod_jk.so
JKWorkersFile /usr/local/apache/conf/workers.properties
# Where to put jk logs
JKLogLevel debug
# Select the log format
JKLogStampFormat "[%a %b %d %H:%M:%S %Y]"
# JKOptions indicate to send SSL KEY SIZE
JKOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
# JKRequestLogFormat set the request format
JKRequestLogFormat "%w %V %T"
# Send all xhtml requests to Glassfish
JKMount /*.xhtml worker1
# Send all domain requests to Glassfish
JKMount /domain/* worker1

<VirtualHost 0.0.0.0:80>
    ServerName sub.domain.com
    ServerAlias www.sub.domain.com
    DocumentRoot /home/sub/public_html/sub
    ServerAdmin webmaster@sub.domain.com
    UseCanonicalName Off
    CustomLog /usr/local/apache/domlogs/sub.domain.com combined
    CustomLog /usr/local/apache/domlogs/sub.domain.com-bytes_log "%{%s}t %I .\n%{%s}t %O ."
    ## User user # Needed for Cpanel::ApacheConf
    UserDir enabled user
    <IfModule mod_suphp.c>
        suPHP_UserGroup user user
    </IfModule>
    <IfModule !mod_disable_suexec.c>
        <IfModule !mod_ruid2.c>
            SuexecUserGroup user user
        </IfModule>
    </IfModule>
    <IfModule mod_ruid2.c>
        RMode config
        RUidGid user user
    </IfModule>
    <IfModule itk.c>
        # For more information on MPM ITK, please read:
        #   http://mpm-itk.sesse.net/
        AssignUserID user user
    </IfModule>
    ScriptAlias /cgi-bin/ /home/user/public_html/sub/cgi-bin/

    AccessFileName .htaccess
</VirtualHost>


<VirtualHost 0.0.0.0:80>  
  ServerAdmin admin@domain.com  
  ServerName  domain.com  

        ProxyRequests Off  
        <Proxy *>  
           Order deny,allow  
           Allow from all  
        </Proxy>
        ProxyPass / http://0.0.0.0:8080/  
        ProxyPassReverse / http://0.0.0.0:8080/  

        <Location />
        Order allow,deny
        Allow from all
        </Location>

       ErrorLog logs/glassfish-apache-error_log  
       CustomLog logs/glassfish-apache-access_log common      
</VirtualHost>

Include "/usr/local/apache/conf/includes/post_virtualhost_global.conf"
Include "/usr/local/apache/conf/includes/post_virtualhost_2.conf"

【问题讨论】:

  • 您能否提供一个触发此规则的示例 URL?另外,您知道是否还有其他.htaccess 规则?
  • 这是一个常见问题。 RewriteRule 非常通用,并且匹配右侧的 URI,从而导致循环。你需要一个RewriteCond 来防止这种情况。通常由 RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-d 完成,以防止将规则应用于真实的现有文件。
  • 如果您想查看,触发此规则的 URL 是 index.php 文件 (companion.gesht.com)。我有这两个RewriteCond 规则。
  • 我添加了整个 .htaccess 文件@anubhava
  • 我用 sub.domain.com 代替了 partner.gesht.com 和 0.0.0.0 代替了我的 IP。

标签: php apache .htaccess


【解决方案1】:

规则重定向到自身。 在您之前尝试以下规则:

RewriteRule ^engine/handlers/page_handler.php  -  [L]

如果服务器为您的 page_handler.php 脚本提供服务,这将结束重定向规则。

【讨论】:

    【解决方案2】:

    你有这两条重写规则

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([A-Za-z0-9\_\-]+)$ engine/handlers/... [QSA,L]
    
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([A-Za-z0-9\_\-]+)\/(.*)$ engine/handlers/... [QSA,L]
    RewriteRule . /~gesht/companion/
    

    第二条规则的重写格式仍然匹配重写表达式,因此该规则将直接循环回到自身。

    为避免这种情况,要么指定 REQUEST_URIREQUEST_URI 不匹配的 RewriteCond,要么添加环境标志以停止循环:

    ...[QSA,L,E=LOOP:1]
    

    现在通过对充当停止块的“LOOP”进行环境检查来抢占规则本身;这必须在任何规则设置之前LOOP:

    # if LOOP is set...
    RewriteCond %{ENV:LOOP} =1
    # ...then no-rewrite and quit.
    RewriteRule .* - [L]
    
    # All others rules follow.
    

    【讨论】:

      【解决方案3】:

      避免这种情况需要做两件事。

      首先要避免无限循环,在RewriteBase 行下方添加此规则

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

      然后要注意 css/js/image 不显示,您只需在 css、js、图像文件中使用绝对路径,而不是相对路径。这意味着您必须确保这些文件的路径以 http:// 或斜杠 / 开头。

      否则:

      您可以尝试在页面的 HTML 标头中添加此内容:

      <base href="/" />
      

      【讨论】:

      • 谢谢 @anubhava 这是一个 elgg 网站,我添加了 &lt;base href="/" /&gt;,但它没有帮助加载 css。我在没有 cPanel 的服务器上和本地机器上都有这个网站,没有任何问题。 cPanel在apache中做了哪些不同的配置会导致这个错误?
      • 文件没有被加载,因为它们像companion.gesht.comcss 而不是companion.gesht.com/css 即使在css和&lt;base href="/"/&gt;之前添加/,问题也没有得到解决。
      • 看起来你有一些错误配置的规则删除了域名后的斜线。
      • 是的,我确实看到在网络 @anubhava 下暂时删除了 302
      • 现在添加斜线后,我会加载 css,但不会加载 js 和图形。当链接正确时,我看到 302 为他们临时移动了消息。
      猜你喜欢
      • 1970-01-01
      • 2013-10-04
      • 1970-01-01
      • 2019-03-08
      • 2018-03-18
      • 2013-06-07
      • 1970-01-01
      • 1970-01-01
      • 2014-09-25
      相关资源
      最近更新 更多