Manoj Damor

Unlock the World of Coding with Coding Funda

How to Change the PHP Version for a Specific Directory in A2 Hosting

How to Change the PHP Version for a Specific Directory in A2 Hosting Manoj Damor

How to Change the PHP Version for a Specific Directory in A2 Hosting Manoj Damor

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

  1. Open your virtual host configuration file: sudo nano /etc/apache2/sites-available/your-site.conf
  2. Update the DocumentRoot directive to point to the public 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>
  3. Enable the site and restart Apache: sudo a2ensite your-site.conf sudo systemctl restart apache2

For Nginx:

  1. Open your Nginx configuration file: sudo nano /etc/nginx/sites-available/your-site
  2. Update the root directive to point to the public 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; } }
  3. 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:

  1. Move Files: Move index.php and .htaccess from the public folder to the root directory of your project.
  2. Update Paths in index.php: Open the index.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';
  3. 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:

  1. Create or update an .htaccess file in your Laravel root directory.
  2. 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

  1. Visit your site in the browser (e.g., http://example.com).
  2. 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:

  1. Block access to the storage and other non-public folders by adding rules to .htaccess or nginx.conf.
  2. 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.

Check My Social Profile Links

Instagram

Youtube

Website

Linkedin

Android Application

About The Author

Leave a Reply

Your email address will not be published. Required fields are marked *

Follow by Email
fb-share-icon
Share