Laravel - How do you redirect all request to public/ folder (.htaccess )

How to redirect your Laravel website from the /public folder to the main domain

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:

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 to 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 than 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 addon domain under your account.

  • laravel, htaccess
  • 4 Users Found This Useful
Was this answer helpful?