Copied!
Laravel
How to remove public from url in laravel in 3 simple steps
post-hover-image-01.jpg
Shahroz Javed
Jun 25, 2023 . 754 views

Table Of Contents

 

Introduction

Laravel, one of the most popular PHP frameworks, provides developers with a robust and efficient platform for web application development. However, by default, Laravel includes a "public" folder in the URL structure, which can make the links appear cluttered and less user-friendly. In this blog post, we will explore the steps to remove the "public" segment from Laravel URLs, resulting in cleaner and more aesthetically pleasing links.

Why Remove "Public" from Laravel URLs?

The inclusion of the "public" folder in Laravel URLs serves a security purpose, as it restricts direct access to important application files. However, in most cases, users are not concerned with the underlying file structure and are more interested in accessing a user-friendly and intuitive URL. Removing "public" from the URL structure helps create a more seamless and professional user experience.

Steps

01: Move .htaccess file

Move the .htaccess file from the public folder to the Laravel project root folder.

02: Create index.php in root

Create a new file index.php in the root folder and copy the code from server.php to index.php that we created in the root folder.

Note:- Do not rename the server.php file to index.php because by doing this you will not able to run artisan commands anymore. If you have any issue feel free to write down in comment.

03: Update .htaccess

Update .htaccess file.

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


RewriteEngine On

RewriteRule ^portfolio(/.*)?$ - [L]


# Block access to files
<FilesMatch "(.env|package.json|package.lock|composer.json|composer.lock|webpack.mix.js|artisan.php)">
    Order allow,deny
    Deny from all
</FilesMatch>

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
          

04: Fix assets isse

To fix the assets path issue update ASSET_URL in .env.

ASSET_URL=http://localhost/project/public

Conclusion:

By following all these steps you will be able to remove the public from url in Laravel.

4 Shares

Similar Posts