1

I am trying to configure virtual hosts with multiple sites with codeigniter based on folders on ubuntu 18 with Apache2. By this what I mean is site1 is to be accessed by using url http://localhost/site1 and site2 is to be accessed by using url http://localhost/site2 and site3 is to be accessed by using url http://localhost/site3 and so on. Content to be served are in folders under /var/www/site1 and /var/www/site2 etc. I can't use name based virtual hosts because the users will be using it over the network not in the local machine.

So I updated default config file on ubuntu like below

<VirtualHost *:80>
    
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www

    Alias /site1 /var/www/site1/html
    <Directory /var/www/site1/html>
            Options +MultiViews -Indexes +Includes +FollowSymLinks
            AllowOverride All
            Require all granted
  </Directory>

    
           Alias /site2 /var/www/site2/html
    <Directory /var/www/site2/html>
            Options +MultiViews -Indexes +Includes +FollowSymLinks
            AllowOverride All
            Require all granted
    </Directory>

  Alias /site3 /var/www/site3/html
    <Directory /var/www/site3/html>
            Options +MultiViews -Indexes +Includes +FollowSymLinks
            AllowOverride All
            Require all granted
  </Directory>

       ErrorLog ${APACHE_LOG_DIR}/error.log
       CustomLog ${APACHE_LOG_DIR}/access.log combined
    
</VirtualHost>

Folder structure is like below /var/www/site1/html /var/www/site2/html /var/www/site3/html etc.

I am able to access site1 normally without any issue. But for site2 and site3 I am getting 500 internal server error with message " Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. " in the logs. When enabled debug mode following messages were in the log.

: r->uri = /site2/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/master/get_announcements, referer: http://192.186.1.4/site2/
[Mon Feb 01 12:07:57.895466 2021] [core:debug] [pid 8335] core.c(3840): [client 192.186.1.3:57079] AH00122: redirected from r->uri = /site2/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/master/get_announcements, referer: http://192.186.1.4/site2/
[Mon Feb 01 12:07:57.895478 2021] [core:debug] [pid 8335] core.c(3840): [client 192.186.1.3:57079] AH00122: redirected from r->uri = /site2/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/master/get_announcements, referer: http://192.186.1.4/site2/
[Mon Feb 01 12:07:57.895490 2021] [core:debug] [pid 8335] core.c(3840): [client 192.186.1.3:57079] AH00122: redirected from r->uri = /site2/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/master/get_announcements, referer: http://192.186.1.4/site2/
[Mon Feb 01 12:07:57.895501 2021] [core:debug] [pid 8335] core.c(3840): [client 192.186.1.3:57079] AH00122: redirected from r->uri = /site2/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/master/get_announcements, referer: http://192.186.1.4/site2/
[Mon Feb 01 12:07:57.895512 2021] [core:debug] [pid 8335] core.c(3840): [client 192.186.1.3:57079] AH00122: redirected from r->uri = /site2/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/master/get_announcements, referer: http://192.186.1.4/site2/
[Mon Feb 01 12:07:57.895523 2021] [core:debug] [pid 8335] core.c(3840): [client 192.186.1.3:57079] AH00122: redirected from r->uri = /site2/html/index.php/html/index.php/html/index.php/html/index.php/master/get_announcements, referer: http://192.186.1.4/site2/
[Mon Feb 01 12:07:57.895534 2021] [core:debug] [pid 8335] core.c(3840): [client 192.186.1.3:57079] AH00122: redirected from r->uri = /site2/html/index.php/html/index.php/html/index.php/master/get_announcements, referer: http://192.186.1.4/site2/
[Mon Feb 01 12:07:57.895562 2021] [core:debug] [pid 8335] core.c(3840): [client 192.186.1.3:57079] AH00122: redirected from r->uri = /site2/html/index.php/html/index.php/master/get_announcements, referer: http://192.186.1.4/site2/
[Mon Feb 01 12:07:57.895573 2021] [core:debug] [pid 8335] core.c(3840): [client 192.186.1.3:57079] AH00122: redirected from r->uri = /site2/html/index.php/master/get_announcements, referer: http://192.186.1.4/site2/
[Mon Feb 01 12:07:57.895583 2021] [core:debug] [pid 8335] core.c(3840): [client 192.186.1.3:57079] AH00122: redirected from r->uri = /site2/master/get_announcements, referer: http://192.186.1.4/site2/

If it helps following is the .htaccess in the /var/www/site2 folder

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On


#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#This last condition enables access to the images and css folders, and the robots.txt file

RewriteCond $1 !^(index\.php|public|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

This same .htaccess is present in all the three directories. But it works only for first one for other 2 it gives error as mentioned above.

Please help me fix this.

Thanks a lot in advance

1 Answers1

0

Short answer: try adding

RewriteBase /site2

in the .htaccess for site2. And similarly for /site1 and /site3.

From the error log, the problem is clearly in the RewriteRule, which is creating an infinite loop of adding /html/index.php to the front of the URL.

The problem is in this condition:

RewriteCond $1 !^(index\.php|public|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [L]

The RewriteCond is trying to exclude URLs that already start with index.php, public, and so on. In a .htaccess file, that doesn't work unless you tell Apache to remove the leading part of the URL with RewriteBase, as above. See the docs for RewriteBase, and also the sections "What is matched?" and "Per-directory Rewrites" in the docs for RewriteRule, although warning, those sections are really hard to understand.

I guess the reason the rule works for /site1 but not the others is that /site1 includes an index.php file, so the condition RewriteCond %{REQUEST_FILENAME} !-f fails and the RewriteRule isn't triggered.

By the way you don't need a <VirtualHost> here, since you're serving all 3 of these directories from the same hostname (http://localhost).

Andrew Schulman
  • 8,811
  • 21
  • 32
  • 47