How to Redirect Laravel in Apache WITHOUT CHANGING URL

The URL will not appear 'yoursite.com/yourprojeto/public' but only: 'yoursite.com'


How to Redirect Laravel in Apache WITHOUT CHANGING URL


Laravel has been frequently used by web developers and many are deploying it on hosting servers with common plans where there is only Apache available in a simple way.

Many times you are making a development version and do not want to remove the files from the root of your project and replace them with the beta version, among many other reasons.

In this article we will see how to use Laravel in Apache as well as how to use it, if you want, in the root of your project even though it is in a subfolder on the server.

So, to access Laravel in your browser, you can use the command:

php artisan serve

Within a Laravel project.

To access directly from Apache, follow the steps below


Accessing Laravel on Apache

First, let’s see how to access Laravel on Apache without needing: ~php artisan serve~.

1. Move your Laravel project to your server:

mv myproject /var/www/html

Or also, depending on your system: mv meuprojeto /var/www/localhost/htdocs, or any other case.

2. Create a configuration file for port 80

sudo vim /etc/apache2/sites-available/laravel.conf

Some systems may have a different path, in my case (Gentoo) it is: sudo vim /etc/apache2/vhosts.d/laravel.conf.

And insert the content below:

<VirtualHost *:80>
     ServerName localhost
     DocumentRoot /var/www/localhost/htdocs/meuprojeto/public

     <Directory /var/www/localhost/htdocs/meuprojeto/public>
         Options Indexes FollowSymLinks
         AllowOverride All
         Require all granted
     </Directory>

     ErrorLog /var/log/apache2/laravel_error.log
     CustomLog /var/log/apache2/laravel_access.log combined
</VirtualHost>

3. Assign all possible permissions to access and edit files

sudo chown -R apache:apache /var/www/html/myproject
sudo chown -R apache:apache /var/log/apache2
sudo chmod -R 775 /var/www/html/myproject/storage/
sudo chmod -R 775 /var/www/html/myproject/bootstrap/cache/
sudo usermod -a -G apache $USER
sudo find /var/www/html/myproject -type f -exec chmod 664 {} \;
sudo find /var/www/html/myproject -type f -exec chmod 775 {} \;
sudo chmod -R gu+w /var/www/html/myproject

4. Restart Apache and access it in the browser

# In some cases where there is the a2enmod command, also run:
sudo a2enmod rewrite
sudo systemctl restart apache2

Use the way to restart Apache according to your system. On mine it is: sudo rc-service apache restart.

Now, access the address corresponding to your project in the browser, but for the public folder:

http://localhost/myproject/public

And note that your project is already running directly on Apache!


Redirection BY CHANGING the URL

You may still want to access the project, but identify in the URL that it was redirected.

For this case, create a file named: .htaccess in the root of your server, example:

vim /var/www/html/.htaccess

And paste the content below, replace the name myproject with the name of your project:

RewriteEngine On

# Redirect http://localhost to http://localhost/myproject/public/
RewriteCond %{HTTP_HOST} ^(localhost)$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /myproject/public/$1 [L,R=301]

There are cases where you may need to restart Apache once again and other cases until you clear your browser’s cache:

sudo systemctl restart apache2

# For example clear Google Chrome cache on Ubuntu
rm -rf ~/.cache/google-chrome

Now test by accessing: http://localhost and you will be redirected to http://localhost/meuprojeto/public


Redirection WITHOUT CHANGING the URL

Perhaps this is the most interesting part of this article, that is, something you won’t find in the Laravel documentation! 😃

If you want to access the project, redirect to myproject/public, but without changing the URL, replace the content of .htaccess with this content:

Change myproject to the name of your project. vim /var/www/html/.htaccess

RewriteEngine On

# Internally rewrite http://localhost to http://localhost/myproject/public/
RewriteCond %{HTTP_HOST} ^localhost$ [NC,OR]
RewriteCond %{HTTP_HOST} ^localhost$
RewriteCond %{REQUEST_URI} !myproject/public/
RewriteRule (.*) /myproject/public/$1 [L]

I hope I helped, if in your case you have any problems, review the steps.

And don’t forget to share this article!


laravel apache webdevelopment php


Share


YouTube channel

Subscribe


Marcos Oliveira

Marcos Oliveira

Software developer
https://github.com/terroo