Laravel is a powerful and flexible PHP framework that makes it easy to develop web applications. When you install Laravel, you might notice that your URLs include the /public
segment, which can look something like this: http://your-domain.com/public/
. Removing /public
from the URL can make your URLs cleaner and more professional.
In this article, we’ll walk through the steps to remove the /public
segment from your Laravel 11 application URLs in an easy-to-understand manner. Let’s get started!
Step 1: Move Files from Public Directory
The first step is to move some essential files from the public
directory to the root directory of your Laravel application.
- Open your Laravel application folder.
- Move the following files from the
public
directory to the root directory:index.php
.htaccess
After moving these files, your folder structure should look something like this:
/your-laravel-app
/app
/bootstrap
/config
/database
/public (now empty)
/resources
/routes
/storage
/tests
/vendor
.env
.htaccess (moved)
artisan
composer.json
index.php (moved)
...
Step 2: Update index.php
Next, we need to update the paths in the index.php
file that we just moved to the root directory. Open index.php
in a text editor and find these two lines:
require __DIR__.'/../vendor/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php';
Change these lines to:
require __DIR__.'/vendor/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';
Step 3: Update .htaccess
The .htaccess
file helps manage the URL rewriting for your Laravel application. Since we moved it to the root directory, we need to ensure it’s correctly set up to handle requests.
Open the .htaccess
file and ensure it contains the following rules:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Disable Directory Listings...
Options -Indexes
</IfModule>
These rules ensure that requests are correctly routed to index.php
without the /public
segment.
Step 4: Update Environment Configuration (Optional)
If you’re using environment variables (defined in your .env
file) for any paths that point to the public
directory, you might need to update them. For example, if you have:
ASSET_URL=http://your-domain.com/public
Change it to:
ASSET_URL=http://your-domain.com
Step 5: Clear Cache and Config
Finally, clear the cache and config to make sure your changes take effect. Run the following Artisan commands in your terminal:
php artisan config:clear
php artisan cache:clear
php artisan route:clear
php artisan view:clear
Conclusion
That’s it! You have successfully removed the /public
segment from your Laravel 11 application URLs. Now your application URLs will look cleaner and more professional.
Here’s a summary of what we did:
- Moved
index.php
and.htaccess
from thepublic
directory to the root directory. - Updated the paths in
index.php
. - Ensured
.htaccess
is correctly set up for URL rewriting. - (Optional) Updated environment configuration if necessary.
- Cleared cache and config.
With these changes, your Laravel 11 application will now handle URLs without the /public
segment, making them much more user-friendly. Happy coding!
- Check our tools small Tools
- Check our tools website Word count