重写规则导致“请求超出了10个内部重定向的限制”

时间:2014-06-28 02:23:35

标签: php apache .htaccess

为什么.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"

3 个答案:

答案 0 :(得分:0)

规则重定向到自身。 在您之前尝试类似以下规则的内容:

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

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

答案 1 :(得分:0)

你有这两个重写规则

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/

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

要避免这种情况,要么指定RewriteCond REQUEST_URI尚未匹配engine/...,要么添加环境标志以停止循环:

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

现在使用针对&#34; LOOP&#34;的Env检查来抢占规则本身。作为一个停止块;这必须在任何规则设置LOOP

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

# All others rules follow.

答案 2 :(得分:0)

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

首先要避免无限循环,请在RewriteBase

下方添加此规则
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^ - [L]

然后为了照顾未显示的css / js / image,你需要在你的css,js,images文件而不是相对文件中使用绝对路径。这意味着您必须确保这些文件的路径以http://或斜杠/开头。

或者其他:

您可以尝试在页面的HTML标题中添加:

<base href="/" />