Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 – Remove Public from URL

People also ask

How can I remove public Index php in the URL generated Laravel?

Create or edit the . htaccess file in the Laravel root directory and configure the RewriteRule to remove “public/index. php” from the URL. You must have mod_rewrite enable on your Apache server.

Where does .htaccess file go Laravel?

htaccess file for redirecting to Laravel's public folder. In Laravel the path for serving your web page is in the /public folder. By default after installing Laravel and navigating in a browser to the URL you will see a directory listing of all the Laravel files.

What is the use of .htaccess file in Laravel?

htaccess file that is used to allow URLs without index. php . If you use Apache to serve your Laravel application, be sure to enable the mod_rewrite module.


For Laravel 5:

  1. Rename server.php in your Laravel root folder to index.php
  2. Copy the .htaccess file from /public directory to your Laravel root folder.

That's it!




PLEASE NOTE when serving a Laravel project with Docker: you won't need to do any of this. Only use this option when your root (or more commonly: public_html) directory of your website is your Laravel project (this is not the case when you're using Docker).

DON'T!

YOU REALLY SHOULD NOT rename server.php in your Laravel root folder to index.php and copy the .htaccess file from the /public directory to your Laravel root folder!!!

This way everyone can access some of your files (.env for example). Try it yourself. You don't want that!


DO

Instead, you should create an .htaccess file in your root like this:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,QSA]

This will silently rewrite all your base URIs to the /public folder. Even all Headers, for example the HTTP Authorization Header, and all optional URI parameters will silently be passed on to the /public folder as well.

That's all


I have solved the issue using 2 answers:

  1. Renaming the server.php to index.php (no modifications)
  2. Copy the .htaccess from public folder to root folder (like rimon.ekjon said below)
  3. Changing .htaccess it a bit as follows for statics:

    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]
    
    RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !^/public/
    RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [L,NC]
    

    If there are any other static files needed just add the extension to the previous declared list


In Laravel 5.5 create .htacess file in your root directory and placed the following code:- Reference Link

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ ^$1 [N]

    RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
    RewriteRule ^(.*)$ public/$1 

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ server.php

</IfModule>