|

How To deploy Laravel project on shared hosting

deployed laravel on cpanel

How To deploy Laravel project on shared hosting or on a cPanel.

No need to move any files from public folder to root, just create .htaccess file and paste below code.

Learn how to deploy a Laravel project on shared hosting by configuring .htaccess to serve the application from the public folder without moving any files to the root directory.

What the below .htaccess file covers:

  • Enable URL rewriting:
  • Redirect to the public folder:
  • Secure File
  • Prevent access to sensitive files
  • Secure other sensitive files like artisan and webpack.mix.js
  • Redirect requests from root to the ‘public’ directory
  • RewriteCond %{REQUEST_URI} (.\w+$) [NC]

create a .htaccess file on a root like public_html. below i attached a screenshot to where to create the .htaccess file

Note : example.com should be replace with your original domain or subdomain name like demo.example.com

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>
    RewriteEngine on

    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ ^$1 [N]
    # RewriteCond %{REQUEST_URI} (\.\w+$) [NC]

    # Redirect requests from root to the 'public' directory
    RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
    RewriteCond %{HTTP_HOST} ^www.example.com$
    RewriteCond %{REQUEST_URI} !^/public/
    RewriteRule (.*) /public/$1 [L]

   #Secure File
   <Files ~ "\.(env|json|config\.js|md|gitignore|gitattributes|lock|editorconfig|yml|styleci\.yml)$">
        Order allow,deny
        Deny from all
    </Files>

    <FilesMatch "^(?!sitemap\.xml$)(.+)\.xml$">
        Order allow,deny
        Deny from all
    </FilesMatch>

    <Files ~ "^(artisan|package\.json|webpack\.mix\.js)$">
        Order allow,deny
        Deny from all
    </Files>

# Deny access to service-account.json and other sensitive files
#<FilesMatch "(^|/)(service-account\.json|\.env)$">
#    Require all denied
#</FilesMatch>

</IfModule>

If you have service-account.json file then uncomment the code from above htaccess file

# Deny access to service-account.json and other sensitive files
<FilesMatch "(^|/)(service-account\.json|\.env)$">
    Require all denied
</FilesMatch>
How To deploy Laravel project on shared hosting
htaccess chnage domain
deployed laravel on cpanel

Optional Improvements:

Force HTTPS: If your site supports HTTPS, you can force all requests to be redirected to HTTPS by adding this rule:

RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Optimize for Laravel performance: You can add caching rules and leverage browser caching by adding:

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access 1 year"
    ExpiresByType image/jpeg "access 1 year"
    ExpiresByType image/gif "access 1 year"
    ExpiresByType image/png "access 1 year"
    ExpiresByType text/css "access 1 month"
    ExpiresByType text/html "access 1 week"
    ExpiresByType application/javascript "access 1 month"
</IfModule>

Prevent access to sensitive files:

<Files ~ "\.(env|json|config\.js|md|gitignore|gitattributes|lock|editorconfig|yml|styleci\.yml)$">
   Order allow,deny
   Deny from all
</Files>

Similar Posts

Leave a Reply

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