Codeigniter 4 Multisite

Viewed 270

I have installed Codeigniter 4 on Raspbian and everything seems to be working fine. My web directory is /var/www/html Inside there are two folders containing two different Codeigniter-4 apps that I would like to invoke with:

  • blue.ddns.net -> /var/www/html/blue/public/index.php
  • black.ddns.net -> /var/www/html/black/public/index.php

So I'm creating a .htaccess file to put in the folder /var/www/html/ to handle the two requests

Well, I'm still at step 0 because I can't get the .htaccess file to work properly.

Below I attach a copy of the file:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /var/www/html
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ blue/public/index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    ErrorDocument 404 /index.php
</IfModule>

The Rewrite module is activated the error that appears in /var/log/apache2/error.log is:

/var/www/html/.htaccess: Expected </IfModule> before end of configuration, referer: https://blue.ddns.net/
2 Answers

That shouldn't really be done in your htaccess. You should setup that in your apache virtual hosts.

Go into your sites-avaiable folder.

$ cd /etc/apache2/sites-available

Create a new virtual host called blue.ddns.net.conf

$ touch blue.ddns.net.conf

Open that file with nano or any other text editor you might like and add the following.

<VirtualHost *:80>
        ServerAdmin your@email.com
        DocumentRoot /var/www/html/blue/public
        ServerName blue.ddns.net
        ServerAlias blue.ddns.net
        <Directory "/var/www/html/blue/public">
                allow from all
                AllowOverride All
                Options None
                Require all granted
        </Directory>
        RewriteEngine on
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
        RewriteCond %{SERVER_NAME} =blue.ddns.net
        RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

Save and close the file.

Then add this to your apache with the following:

$ sudo micro blue.ddns.net.conf

Repeat the same process for the other codeigniter install and reboot apache.

$ sudo service apache2 restart

That should do it.

Now if you want to override some config that would be something that you might do in your htaccess file for each codeigniter 4 install.

I've always worked in hosting and never had to do Apache configurations. But now I'm discovering the Raspberry/Linux world. Thanks to your answers I was able to better understand the problem and solve it (for the moment) in the following way:

Now my configuration files 000-default.conf (port 80) and 000-default-le-ssl.conf (port 443) both contain the following code:

<Directory "/var/www/html">.
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

in /var/www/html instead I created a .htaccess file with the following code:

# Disable directory browsing
Options All -Indexes

<IfModule mod_rewrite.c>.
    RewriteEngine On

    RewriteCond "%{HTTP_HOST}" "black\.ddns\.net"
    RewriteRule ^(.*)$ black/public/index.php?/$1 [L]

    RewriteCond "%{HTTP_HOST}" "blue\.ddns\.net"
    RewriteRule ^(.*)$ blue/public/index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    ErrorDocument 404 /index.php
</IfModule>

apps are located in

/var/www/html/blue

and /var/www/html/black

It's a very simple solution that serves my purposes. Although I think it's not very elegant What do you think about it?

Related