2

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/
MrWhite
  • 43,179
  • 8
  • 60
  • 84
360degree
  • 77
  • 1
  • 6
  • Please add your server config / vHost container(s) to your question. Are you attempting to do this with a single vHost for both hostnames (as it looks like here)? Or separate vHost configs for each hostname (which is recommended and assumed by the current answer)? – MrWhite Mar 25 '21 at 19:11
  • But the error you are seeing suggests you are missing a closing ` – MrWhite Mar 25 '21 at 19:16
  • You should also remove the `RewriteBase` directive entirely. The argument to the `RewriteBase` directive is a URL-path, not a filesystem path. See https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase – MrWhite Mar 25 '21 at 19:18

2 Answers2

2

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.

marcogmonteiro
  • 2,061
  • 1
  • 17
  • 26
  • Thank you for your response. I followed your mini-tutorial but nothing seems to change: Both calling https://blue.ddns.net and calling https://black.ddns.net I always see the same thing: my /var/www/html (browsable) folder. I've tried both with and without an .htaccess file in the /var/www/html folder – 360degree Mar 25 '21 at 17:38
  • This answer does assume you have an SSL cert installed and another `` container that actually does the work. Other notes on the `` container as posted... the `ServerAlias` directive is superfluous. You shouldn't be using `Allow from all` _and_ `Require all granted`. Assuming this is Apache 2.4 then only the later is required. `Options None` - should be `Options FollowSymLinks` since mod_rewrite is being used. But mod_rewrite is not required here (you certainly don't need to check the `SERVER_NAME`), a simple `Redirect` is sufficient and preferred. – MrWhite Mar 25 '21 at 18:09
  • @MrWhite: thanks for the answer. Yes, I have installed Let'sEncrypt with a permanent redirect from HTTP to HTTPS – 360degree Mar 25 '21 at 18:44
  • @360degree From the suggestion posted here, `/var/www/html` should not be browsable and your `.htaccess` file should go in the respective `DocumentRoot`, not `/var/www/html`. (Obviously, you'll need to adjust your `.htaccess` file accordingly.) – MrWhite Mar 25 '21 at 19:16
  • @MrWhite: I currently no longer have an htaccess (nothing changed). Sorry but this is the first time I've dealt with Apache a little more in depth. I seriously need a hand to create it properly. As long as there is a need to create it – 360degree Mar 25 '21 at 19:58
  • If you redirected your traffic to https your virtual hosts must be configured to port 443 and not 80. – marcogmonteiro Mar 26 '21 at 08:24
2

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?

360degree
  • 77
  • 1
  • 6
  • So, you're not using _any_ virtual hosts? – MrWhite Mar 26 '21 at 10:58
  • I tried to use a virtual host with no success. I'll try again. For now my .htaccess doesn't work if I invoke an existing file. Even if I put RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d – 360degree Mar 26 '21 at 11:06
  • Virtual hosts are really essential if you want to have more than 1 "separate" website. Those `RewriteCond` directives would need to go before each `RewriteRule` in your code above. – MrWhite Mar 26 '21 at 11:59