How to Redirect all Requests to the public/ folder in Laravel

If you have installed Laravel on your primary domain for the account in your public_html folder (root directory for the primary domain), but you do not want your visitors to see the default domain.com/public url, follow these steps:

Symlink way (recommended)

1. Let's assume that you have a Laravel application in /home/user/laravel  directory.  (user is your hosting account username)

  • SSH to your server or use Terminal in Cpanel and cd to home by cd ~.
  • Back up public_html directory if you have any important file in it.
  • Delete public_html directory by running rm -rf public_html.

2. Now create a symbolic link with name public_html point to your laravel applications public directory by running


    ln -s /home/user/laravel/public public_html

  • Now, the public_html is just a symbolic link of your application's public directory.
  • If anything change in your applications public directory, it will be immediately available for user.

 

.htaccess way

Look for .htaccess files in your public_html (or your domain root folder). By default, dotfiles are hidden in your File Manager. To view all dotfiles, Log into your cPanel and click the File Manager, where you will be able to see all the files in your account. To display the hidden files (also called "dot" files), click "Settings" button in the upper right corner of the files manager. If you still cannot find the .htaccess file, create a New File from File Manager, named .htaccess.
After you open the .htaccess file, add the following code:


<IfModule mod_rewrite.c>

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

RewriteEngine On
# Prevent direct access to the "public" folder - redirect to root
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /public/
RewriteRule ^public/(.*) /$1 [R=302,L]

# Redirect Trailing Slashes If Not A Folder...
# - but look for the file in the "public" folder
# (ensure we are not already in the "public" folder)
RewriteCond %{REQUEST_URI} !^/public/
RewriteCond %{DOCUMENT_ROOT}/public/$1 !-d
RewriteRule ^(.*)/$ /$1 [R=302,L]

# Rewrite "everything" to the "public" subdirectory if not already
# This ignores existing files/dirs in the document root
RewriteCond %{REQUEST_URI} ^/(.*)
RewriteRule !^public/ public/%1

# Handle Front Controller... (as before)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

Click "Save Your Changes"

After this, you and your visitors should be able to access your Laravel public folder via domain.com. Keep in mind that you might have to clear your browser cache

In case you wish to install Laravel in a folder different from your public_html (for example if you already have a different site on your primary domain name), we strongly advise that you create a subdomain for it (for instance laravel.domain.com). After you install Laravel on the newly created subdomain, you can follow the steps above. This is also valid if you have installed Laravel on an add-on domain under your account.

  • laravel, htaccess
  • 9868 Users Found This Useful
這篇文章有幫助嗎?

相關文章

How to Create a Full cPanel Backup

1. From the home page of your HayHost client area, navigate to the "Your Active...

How to Install WordPress Manually Using cPanel

1. From the home page of your HayHost client area, navigate to the "Your Active...

How to Create a Database in cPanel (MySQL Database)

1. From the home page of your HayHost client area, then click  "SERVICES".   2. In the "My...

How to Create a Subdomain in cPanel

This article will go over creating a Subdomain under your cPanel account. Log into cPanel on...