Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

hsntngr's avatar

How to deploy laravel on shared hosting properly

I'm trying to deploy a laravel project on shared hosting. Everything goes well but there is problem with htaccess configurations.. both / and /public shows homepage. And sometimes sessions losts

Here is public_html/.htaccess file


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

    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ ^ [N]

    RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
    RewriteRule ^(.*)$ public/

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ server.php

    RewriteCond %{SERVER_NAME} !^www\.
    RewriteRule ^(.*)$ https://www.%{SERVER_NAME}%{REQUEST_URI} [L,R]
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]

</IfModule>

What is my mistake here ?

0 likes
11 replies
Snapey's avatar

Don't do it. You will find that you can just open the .env file in the browser.

Sinnbeck's avatar

You can try this guide but I am with @snapey on this one. Laravel and shared servers don't play well together

hsntngr's avatar

Excuse me but do what ? Does it wrong ?

Snapey's avatar
Snapey
Best Answer
Level 122

I mean don't put the whole project in the published folder of your hosting provider.

Some of your project should be private. You can usually put everything above the folder that the host says put your code in, and then copy the public folder to public_html or htdocs

Sinnbeck's avatar

Interesting idea @snapey. Didn't think of that. @hsntngr you might need to change the public directory then. Can be done like this in your AppServiceProvider

/**
 * Register any application services.
 *
 * @return void
 */
public function register()
{
    $this->app->bind('path.public', function() {
        return base_path('public_html');
    });
}
hsntngr's avatar

@sinnbeck if I bind public_html/ as public folder, Isn't it expose all my files like .env ?

Sinnbeck's avatar

The public folder is supposed to be public. This just enables you to use /public_html instead of /public (I expect the folder on the shared server is called something like this?)

hsntngr's avatar

@snapey I'm really appreciate for the solution but what about symbolic link ? How can I define symbolic link without terminal. console routes is not enough because it creates symbolic link benath the /app/public/storage not public_html/storage.. Is there any elegant way to achieve this ?

Snapey's avatar

You really start to see the limitations of shared hosting when they don't provide SSH access. Unfortunately it probably means that you have to save media to the public folder and not to the storage folder (with a symlink to public). You have to adapt your application to the constraints of the hosting.

hsntngr's avatar

I see, thank you for help, I figure it out though cron jobs

Please or to participate in this conversation.