Solved - 500 Internal Server Error Apache, htaccess redirect

8,907

Yes, there is a rewrite loop for every URI coming from a subdomain here that doesn't map to a filename or directory.

For example if the client comes in with mail.indst.eu/nofile

  • Hit subdomain rule mail.indst.eu for condition !^/mail -> rewrite to mail.indst.eu/mail/nofile
  • No longer matches !^/mail, doesn't hit any external redirect rules, so falls through towards the end.
    • Doesn't match the rewrite exclusion for existing files or one of the hardcoded exclusions
    • Hits the non-host-bound all-uri rule, gets rewritten to mail.indst.eu/index.php
  • Now it matches again the subdomain rule mail.indst.eu for condition !^/mail -> rewrite to mail.indst.eu/mail/index.php
  • No longer matches !^/mail, doesn't hit any external redirect rules, so falls through towards the end.
  • Hits the non-host-bound all-uri rule, apparently mail/index.php is not an existing file, so /mail/index.php gets rewritten to /index.php, now we are in an alternating repeat between /index.php and /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...

If you place an index.php in the subdirectories used by the subdomains, then a non-existing file uri on a subdomain will end up at the matching subdomain/index.php files, which for some applications may be what you want. If you want a serverlevel 404 response, then you have to exclude the subdomain hosts from the catchall rewrite to index.php.

For example

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

OR, which might be more succint, and allow more subdomains:

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

Then a non existing uri at mail.indst.eu/starting-path is server level non existing if it doesn't exist in mail/starting-path

There are other techniques to prevent such loops. If you know that there would always at most be one internal redirect for example, and all rewriterules reside in this one .htaccess then you can do this as first rewriterule in your .htaccess:

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

That stops further internal redirects from this .htaccess. But it would require you to put the external redirect rules that you have before the internal redirect rules. It would not stop the RedirectMatch rules.

Or, if you know that a catchall rewrite to index.php should always be the last word the end mod_rewrite processing with [END]. But in this case that would mean a 404 on the subdomain ends up with the main directory index.php which may not be what you want.

RewriteRule ^.*$ index.php [END]
Share:
8,907

Related videos on Youtube

Elmond
Author by

Elmond

Updated on September 18, 2022

Comments

  • Elmond
    Elmond over 1 year

    So, when i visit a page that is not existing on the main domain www or not i just get the normal 404 error, however when i visit it on a subdomain like dsfds.test.com i get the error 500 and in the log I have this:

    [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.
    

    This is my 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
    

    I have many different Website on my VPS with many subdomain redirects.

    This is the web error:

    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
    

    Thank you very much for your help.

    • Gerrit
      Gerrit about 4 years
      The presented .htaccess does not at first glance lead into recursion. But there is probably some vhost configuration for the subdomains and possibly further .htaccess files in the subdir prefixes you defined that could combined lead into recursion. The internal redirect recursion happens with one of the subdomains mentioned in this .htaccess file?
    • Elmond
      Elmond about 4 years
      @user188737 Yes, it only happen with subdomains listed in the htaccess.
  • Elmond
    Elmond about 4 years
    Thank you, solved.
  • Gerrit
    Gerrit about 4 years
    Thanks. Please accept the answer for future reference.