Skip to content

Instantly share code, notes, and snippets.

@bladeSk
Last active June 12, 2024 14:40
Show Gist options
  • Save bladeSk/3666d04964e4de9c263776ba51f33a18 to your computer and use it in GitHub Desktop.
Save bladeSk/3666d04964e4de9c263776ba51f33a18 to your computer and use it in GitHub Desktop.
Deploying Laravel on a shared hosting using only .htaccess

Deploying Laravel on a shared hosting using only .htaccess

Making Laravel work on a shared hosting can be troublesome, because Laravel needs to have its document root set to the public directory. This may not be configurable by a user or even desirable, when the server is hosting multiple websites.

Here's a simple method using only a .htaccess file placed in Laravel's root directory - e.g. alongside app, bootstrap, config, ... No changes whatsoever are necessary to your code.

The file rewrites all the requests so that requesting /file.png would in fact return /public/file.png and anything else is routed to /public/index.php. This also ensures that nothing outside the public folder can be accessed, thereby protecting any sensitive files like .env or database/*.

The simple method

This method assumes that DOCUMENT_ROOT is set properly by your Apache server. Try this first and use the 2nd method only if it doesn't work on your server.

.htaccess

RewriteEngine on

# serve existing files in the /public folder as if they were in /
RewriteCond %{DOCUMENT_ROOT}public%{REQUEST_URI} -f
RewriteRule (.+) /public/$1 [L]

# route everything else to /public/index.php
RewriteRule ^ /public/index.php [L]

The slightly more complicated method

If your server doesn't set DOCUMENT_ROOT properly, then you'll need to use an absolute path in RewriteCond. That is, an absolute path on the server's filesystem. You can get it by copying the following script to the directory where your Laravel installation will reside and visiting its URL - i.e. http://example.com/get_doc_root.php.

get_doc_root.php

<?php
echo getcwd();

This should result in something like /var/www/example.com/web. Use the following .htaccess file and replace [[your path]] with the actual path you got.

.htaccess

RewriteEngine on

# serve existing files in the /public folder as if they were in /
RewriteCond [[your path]]/public%{REQUEST_URI} -f
RewriteRule (.+) /public/$1 [L]

# route everything else to /public/index.php
RewriteRule ^ /public/index.php [L]

In our example case, the RewriteCond line would look like this:

RewriteCond /var/www/example.com/web/public%{REQUEST_URI} -f
@azizullahaziz
Copy link

none of the methods you explained not working for me

@marwan2
Copy link

marwan2 commented Sep 21, 2019

Thanks, 2nd method works for me

@kosciCZ
Copy link

kosciCZ commented Apr 13, 2020

Hey, this worked great for me (first method), but I had trouble at first, when I was trying to setup this for a subdomain, seems like a stupid mistake but it took me way too long to figure it out. So if anybody else is struggling with this:
RewriteCond %{DOCUMENT_ROOT}/subdomain/public%{REQUEST_URI} -f
This is assuming that you have a folder in your document root that acts as a subdomain

@mikeSxz
Copy link

mikeSxz commented Dec 27, 2020

First method fixed redirections but :-( now styles are not loading, any idea what i'm doing wrong?

@dropways
Copy link

First method fixed redirections but :-( now styles are not loading, any idea what i'm doing wrong?

Same problem

@Amasiani
Copy link

Amasiani commented Mar 8, 2022

it did not work for me after following the steps my page showing the index files my project folder and cgi-bin. Please help

@Porrapat
Copy link

Porrapat commented Dec 9, 2022

Thank you, 2nd method works for me.

@ELSESaid
Copy link

ELSESaid commented Jan 3, 2023

First method fixed redirections but :-( now styles are not loading, any idea what i'm doing wrong?

Removing this line worked for me:
RewriteCond %{DOCUMENT_ROOT}public%{REQUEST_URI} -f

My .htaccess looks like this now:

RewriteEngine on

# serve existing files in the /public folder as if they were in /
RewriteRule (.+) /public/$1 [L]

# route everything else to /public/index.php
RewriteRule ^ /public/index.php [L]

@mohibbulla-munshi
Copy link

its work by first method
thank you

@tinhthaison
Copy link

thks. I use 2nd method and sucsess

@tinhthaison
Copy link

pleas help me, the link bootstrap "domain/bootstrap/css/bootstrap.min.css" not active. "domain/public/bootstrap/css/bootstrap.min.css" active good

@benboubekeur
Copy link

The first method worked

@aryoprakarsa
Copy link

Thanks the 2nd method are worked out

@tinhthaison
Copy link

sr but how to symlink on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment