Search code examples
apache.htaccessinternal-server-error

<VirtualHost> causes our Apache server to produce "internal error"


When I added the <VirtualHost> directive to the below .htaccess file, our production server news.XXX (I write XXX instead of our real domain name) fails with an internal error.

Please help to understand the reason of the error.

Header set X-UA-Compatible "IE=edge"
AddType text/html .html
AddType text/x-component .htc
AddHandler server-parsed .html .xml
Options -Indexes +Includes +ExecCGI
Order allow,deny
Allow from all


#<FilesMatch "\.(xml|css|js)$">
#  ExpiresActive on
#  ExpiresDefault "access plus 15 minutes"
#</FilesMatch>

#<IfModule mod_expires.c>
#  ExpiresActive on
#  ExpiresDefault "access"
#  ExpiresDefault "access plus 15 minutes"
#  ExpiresByType text/html "access"
#  ExpiresByType text/plain "access"
#  ExpiresByType text/csv "access"
#  ExpiresByType application/xml "access"
#</IfModule>


RewriteEngine On

#DirectoryIndex /working-on-server.html
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule ^(.*)$ /working-on-server.html [L]

# Replaced real domain with XXX
<VirtualHost victor11.XXX>
DirectoryIndex /cgi-bin/news/index.NEW.fcgi
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /cgi-bin/news/index.NEW.fcgi [L,QSA]
</VirtualHost>

DirectoryIndex /cgi-bin/news/index.fcgi
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /cgi-bin/news/index.fcgi [L,QSA]

Solution

  • VirtualHost directives cannot be used inside .htaccess files as Apache has already determined which VirtualHost is being used before including the .htaccess rules.

    You may want to do something like this if you have multiple VirtualHosts using the same public html folder:

    <If "req('Host') == 'new.example.com'">
        DirectoryIndex /cgi-bin/news/index.NEW.fcgi
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ /cgi-bin/news/index.NEW.fcgi [L,QSA]
    </If>
    <Else>
        DirectoryIndex /cgi-bin/news/index.fcgi
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ /cgi-bin/news/index.fcgi [L,QSA]
    </Else>