4

I've been trying to deploy my codeigniter 4 app into a local apache server.

I want to be able to access my app through http://localhost/my_app/.

I've set up the url in the .env file:

app.baseURL = 'http://localhost/my_app'

Edited the .htaccess inside the public/ folder

RewriteBase /public/

And created the config for apache

<Directory "/var/www/html/my_app/public">
    AllowOverride all
    LogLevel debug
</Directory>

Alias /my_app /var/www/html/my_app/public

I can access the home page http://localhost/my_app, but when I try to go to another page http://localhost/my_app/bootstrapview, apache can't find the url.

"The requested URL was not found on this server."

Have I missed something to make it work? I don't want to change the position of the index.php file and it is most of the things I've found in my searching.

When I try it with the spark server, it works well.

Edit: I'm using Ubuntu 18.04 and mod_rewrite is enabled.

If I don't use RewriteBase, it keeps getting redirected:

redirected from r->uri = /my_app/public/index.php/public/index.php/public/index.php/public/index.php/public/index.php/bootstrapview 
AusiasMart
  • 53
  • 1
  • 5

2 Answers2

0

I have found some answers while checking out other options.

With this configuration, it will work if you change the RewriteBase to the URL of the app:

RewriteBase /my_app/

There is still a problem with redirections to the base URL "http://localhost/my_app/" in which apache will not find the base route (in Routes.php -> "/") but you can create another route to point to it ("/home") for example.

AusiasMart
  • 53
  • 1
  • 5
0

You should always create virtual hosts for your app. This way instead of working on localhost/foo your would be working on something like http://myapp.local and this way you can mimic a production environment way better and not have problems with uri segments and stuff like that.

To do this you must do the following.

First Register the local domin myapp.local in your local environment

$ sudo nano /etc/hosts 

add the line

127.0.0.1 myapp.local

Then you should update your virtual host to the following:

<VirtualHost *:80>
    DocumentRoot /var/www/html/my_app/public
    ServerName myapp.local
    <Directory "/var/www/html/my_app/public">
        allow from all
        Options None
        Require all granted
    </Directory>
    RewriteEngine on
</VirtualHost>

After all this you MUST reboot your apache service

$ sudo service apache2 restart

Now you can change your app to work with this local domain and you should do this for every app your need to work on your local apache environment.

ps. You local domain can be whaterver you want it to be. myapp.local is just an example, it could be myapp.dev, myapp.loc anything you want.

marcogmonteiro
  • 2,061
  • 1
  • 17
  • 26