How to Change the PHP Version for a Specific Directory in A2 Hosting
How to Remove “Public” from URL in Laravel 11
When you install Laravel, by default, the application is served from the /public
directory. This means URLs look like http://example.com/public
. To make your Laravel 11 application accessible directly via http://example.com
, you need to remove “public” from the URL. Below are the steps to achieve this:
1. Update the Web Server Configuration
You need to configure your web server (Apache or Nginx) to point to the /public
folder.
For Apache:
How to Create an IAM User in AWS
- Open your virtual host configuration file:
sudo nano /etc/apache2/sites-available/your-site.conf
- Update the
DocumentRoot
directive to point to thepublic
folder of your Laravel project:<VirtualHost *:80> ServerName example.com DocumentRoot /var/www/laravel-project/public <Directory /var/www/laravel-project/public> AllowOverride All Require all granted </Directory> </VirtualHost>
- Enable the site and restart Apache:
sudo a2ensite your-site.conf sudo systemctl restart apache2
For Nginx:
- Open your Nginx configuration file:
sudo nano /etc/nginx/sites-available/your-site
- Update the
root
directive to point to thepublic
folder:server { listen 80; server_name example.com; root /var/www/laravel-project/public; index index.php index.html; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php8.1-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
- Restart Nginx:
sudo systemctl restart nginx
2. Move index.php
and .htaccess
(Optional Method)
If you don’t have access to the web server configuration, you can adjust your Laravel project structure.
Steps:
- Move Files: Move
index.php
and.htaccess
from thepublic
folder to the root directory of your project. - Update Paths in
index.php
: Open theindex.php
file and update the following lines: Before:require __DIR__.'/../vendor/autoload.php'; $app = require_once __DIR__.'/../bootstrap/app.php';
After:require __DIR__.'/vendor/autoload.php'; $app = require_once __DIR__.'/bootstrap/app.php';
- Set Proper Permissions: Ensure your root directory and
storage
folder are writable by the web server.
3. Use an .htaccess
Rewrite Rule
If you’re using Apache and cannot modify the virtual host configuration, use an .htaccess
file to rewrite the URL.
Steps:
- Create or update an
.htaccess
file in your Laravel root directory. - Add the following code:
<IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^(.*)$ public/$1 [L] </IfModule>
4. Configure Laravel’s APP_URL
After making the above changes, update the APP_URL
in your .env
file to reflect the new URL:
APP_URL=http://example.com
Run the following command to clear and cache the configuration:
php artisan config:clear
php artisan config:cache
5. Test the Setup
- Visit your site in the browser (e.g.,
http://example.com
). - Verify that the Laravel app is loading without the
/public
segment.
6. Additional Security
If you move files outside the public
folder, your application might expose sensitive files. To secure your Laravel app:
- Block access to the
storage
and other non-public folders by adding rules to.htaccess
ornginx.conf
. - Always serve the application via the
/public
folder when possible.
Conclusion
Removing “public” from the URL in Laravel 11 involves updating the web server configuration or using .htaccess
rewrites. While moving index.php
is an option, updating the web server configuration is the recommended and secure approach.