Web-Server

已解決 - 500 內部伺服器錯誤 Apache,htaccess 重定向

  • February 10, 2020

因此,當我訪問主域 www 上不存在的頁面時,我只會收到正常的 404 錯誤,但是當我在 dsfds.test.com 之類的子域上訪問它時,我會收到錯誤 500,並且在日誌中我擁有這個:

[Thu Feb 06 15:46:41.935207 2020] [core:error] [pid 4744] [client 162.158.154.254:32186] 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.

這是我的 htaccess:

#    Mod_security can interfere with uploading of content such as attachments. If you
#    cannot attach files, remove the "#" from the lines below.
#<IfModule mod_security.c>
#    SecFilterEngine Off
#    SecFilterScanPOST Off
#</IfModule>

ErrorDocument 401 default
ErrorDocument 403 default
ErrorDocument 404 default
ErrorDocument 500 default

<IfModule mod_rewrite.c>
RewriteEngine On

#    If you are having problems with the rewrite rules, remove the "#" from the
#    line that begins "RewriteBase" below. You will also have to change the path
#    of the rewrite to reflect the path to your XenForo installation.
#RewriteBase /xenforo

#    This line may be needed to enable WebDAV editing with PHP as a CGI.
#RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


RewriteCond %{HTTP_HOST} ^alphanetworkmc\.com$
RewriteCond %{REQUEST_URI} !^/anm
RewriteRule ^(.*)$ /anm/$1 [L] 

RewriteCond %{HTTP_HOST} ^www\.alphanetworkmc\.com$
RewriteCond %{REQUEST_URI} !^/anm
RewriteRule ^(.*)$ /anm/$1 [L] 

RewriteCond %{HTTP_HOST} ^analytics\.indst\.eu$
RewriteCond %{REQUEST_URI} !^/sa
RewriteRule ^(.*)$ /sa/$1 [L] 

RewriteRule ^favicon.ico favicon.ico [L]

RewriteCond %{HTTP_HOST} ^mail\.indst\.eu$
RewriteCond %{REQUEST_URI} !^/mail
RewriteRule ^(.*)$ /mail/$1 [L] 

RewriteCond %{HTTP_HOST} ^entertainment\.indst\.eu$
RewriteCond %{REQUEST_URI} !^/ent
RewriteRule ^(.*)$ /ent/$1 [L] 

RewriteCond %{HTTP_HOST} ^www\.statesanalytics\.com$
RewriteCond %{REQUEST_URI} !^/sa
RewriteRule ^(.*)$ /sa/$1 [L] 

RewriteCond %{HTTP_HOST} ^statesanalytics\.com$
RewriteCond %{REQUEST_URI} !^/sa
RewriteRule ^(.*)$ /sa/$1 [L] 

RewriteCond %{HTTP_HOST} ^recover\.indst\.eu$
RewriteCond %{REQUEST_URI} !^/mail/admin/
RewriteRule ^(.*)$ /mail/admin/$1 [L] 

RewriteCond %{HTTP_HOST} ^adminmail\.indst\.eu$
RewriteCond %{REQUEST_URI} !^/mail/admin/
RewriteRule ^(.*)$ /mail/admin/$1 [L] 

<If "req('Host') == 'recover.indst.eu'">
RedirectMatch 301 ^/mail/admin/users/login\.php$ /users/password-recover.php
RedirectMatch 301 ^/mail/admin/login\.php$ /users/password-recover.php
</if>

RewriteCond %{HTTP_HOST} mailrules\.indst\.eu$
RewriteRule ^(.*)$ https://www.alphanetworkmc.com/threads/e-mail-tos.21/$1 [L,R=301]


RewriteCond %{HTTP_HOST} ^mail\.alphanetworkmc\.com$
RewriteCond %{REQUEST_URI} !^/mail
RewriteRule ^(.*)$ /mail/$1 [L] 

RewriteCond %{HTTP_HOST} ^mail\.alphanetworkmc\.com [NC]
RewriteRule ^(.*)$ https://mail.indst.eu/$1 [R,L]

RewriteCond %{HTTP_HOST} ^bans\.alphanetworkmc\.com$
RewriteCond %{REQUEST_URI} !^/anm/bans
RewriteRule ^(.*)$ /anm/bans/$1 [L] 

RewriteCond %{HTTP_HOST} ^recover\.alphanetworkmc\.com$
RewriteCond %{REQUEST_URI} !^/mail/admin/
RewriteRule ^(.*)$ /mail/admin/$1 [L] 

RewriteCond %{HTTP_HOST} ^recover\.alphanetworkmc\.com [NC]
RewriteRule ^(.*)$ https://recover.indst.eu/$1 [R,L]

RewriteCond %{HTTP_HOST} ^adminmail\.alphanetworkmc\.com$
RewriteCond %{REQUEST_URI} !^/mail/admin/
RewriteRule ^(.*)$ /mail/admin/$1 [L] 

RewriteCond %{HTTP_HOST} ^adminmail\.alphanetworkmc\.com [NC]
RewriteRule ^(.*)$ https://adminmail.indst.eu/$1 [R,L]

<If "req('Host') == 'recover.alphanetworkmc.com'">
RedirectMatch 301 ^/mail/admin/users/login\.php$ /users/password-recover.php
RedirectMatch 301 ^/mail/admin/login\.php$ /users/password-recover.php
</if>

<If "req('Host') == 'recover.indst.eu'">
RedirectMatch 301 ^/mail/admin/users/login\.php$ /users/password-recover.php
RedirectMatch 301 ^/mail/admin/login\.php$ /users/password-recover.php
</if>

RewriteCond %{HTTP_HOST} mailrules\.alphanetworkmc\.com$
RewriteRule ^(.*)$ https://www.alphanetworkmc.com/threads/e-mail-tos.21/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^mailrules\.alphanetworkmc\.com [NC]
RewriteRule ^(.*)$ https://mailrules.indst.eu/$1 [R,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
</IfModule>

# php -- BEGIN cPanel-generated handler, do not edit
#Set the “ea-php70” package as the default “PHP” programming language.
<IfModule mime_module>
AddType application/x-httpd-ea-php70___lsphp .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit

我的 VPS 上有許多不同的網站,其中包含許多子域重定向。

這是網路錯誤:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at webmaster@localhost to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.
Apache/2.4.25 (Debian) Server at entertainment.indst.eu Port 80

非常感謝您的幫助。

是的,來自此處子域的每個 URI 都有一個重寫循環,它不映射到文件名或目錄。

例如,如果客戶端帶有mail.indst.eu/nofile

  • 點擊子域規則 mail.indst.eu 獲取條件!^/mail -> 重寫為 mail.indst.eu/mail/nofile

  • 不再匹配 !^/mail,不符合任何外部重定向規則,因此最終失敗。

    • 與現有文件的重寫排除或硬編碼排除之一不匹配
    • 命中非主機綁定的 all-uri 規則,被重寫為 mail.indst.eu/index.php
  • 現在它再次匹配子域規則 mail.indst.eu 條件!^/mail -> 重寫為 mail.indst.eu/mail/index.php

  • 不再匹配 !^/mail,不符合任何外部重定向規則,因此最終失敗。

  • 命中非主機綁定的 all-uri 規則,顯然 mail/index.php 不是現有文件,因此 /mail/index.php 被重寫為 /index.php,現在我們在 /index 之間交替重複。 php 和 /mail/index.php


/nofile
/mail/nofile (does not exist)
/index.php
/mail/index.php (does not exist)
/index.php
/mail/index.php (does not exist)
...repeat last two...

如果您將 index.php 放在子域使用的子目錄中,那麼子域上不存在的文件 uri 將最終出現在匹配的 subdomain/index.php 文件中,這對於某些應用程序可能是您想要的。如果您想要一個伺服器級別的 404 響應,那麼您必須從對 index.php 的全部重寫中排除子域主機。

例如

RewriteCond %{HTTP_HOST} !^mail\.indst\.eu$
RewriteCond %{HTTP_HOST} !^www\.statesanalytics\.com$
RewriteRule ^.*$ index.php [NC,L]

或者,這可能更簡潔,並允許更多子域:

RewriteCond %{REQUEST_URI} !^/mail
RewriteCond %{REQUEST_URI} !^/sa
RewriteRule ^.*$ index.php [NC,L]

然後,如果 mail/starting-path 中不存在 mail.indst.eu/starting-path 中不存在的 uri,則伺服器級別不存在

還有其他技術可以防止此類循環。例如,如果您知道最多總會有一個內部重定向,並且所有重寫器都駐留在這個 .htaccess 中,那麼您可以將其作為.htaccess 中的第一個重寫器來執行:

RewriteCond %{ENV:REDIRECT_STATUS} !^$
RewriteRule ^(.*)$ - [L]

這會阻止來自此 .htaccess 的進一步內部重定向。但這需要您將您擁有的外部重定向規則放在內部重定向規則之前。它不會停止 RedirectMatch 規則。

或者,如果您知道對 index.php 的全面重寫應該始終是結束 mod_rewrite 處理的最後一句話

$$ END $$. 但在這種情況下,這意味著子域上的 404 最終會出現在主目錄 index.php 中,這可能不是您想要的。

RewriteRule ^.*$ index.php [END]

引用自:https://serverfault.com/questions/1002059