Forked from bladeSk/laravel-on-shared-hosting-htaccess.md
Created
June 14, 2019 08:13
-
-
Save Hi7cl4w/96b51b2f4d22dcba644579cb4c9da2c2 to your computer and use it in GitHub Desktop.
Revisions
-
bladeSk revised this gist
Apr 29, 2017 . 1 changed file with 4 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,13 +1,13 @@ # 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. @@ -23,10 +23,7 @@ This method assumes that `DOCUMENT_ROOT` is set properly by your Apache server. 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`. -
bladeSk created this gist
Apr 29, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,53 @@ 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