0

I've built a Laravel site locally, and everything works wonderfully—but when I've tried deploying it to my shared host (Hostinger), I cannot get everything replicated or set up correctly. The obvious problem is that Hostinger puts the public-facing code in a public_html folder, which my Laravel setup does not have (it has public instead).

Also, I'm using GitHub for version control and (hopefully) deployment. I'm able to connect my GitHub repo directly in Hostinger, but the problem is, it pulls my code into public_html instead of the root.

I'm really sorry if this question/problem is dumb; I'm more of a web designer than developer these days, and I just need a damn portfolio site up and running. :)

Local directory structure:

root
--app
--bootstrap
--config
--database
--node_modules
--public
--resources
--routes
--storage
--tests
--vendor

I've read various articles and posts about either copying contents of public into public_html after uploading the code, and then changing __DIR__ suffixes in index.php; or ways to change public to public_html locally up front...but the former didn't work and the latter scares me since so much of my Laravel workflow using npm run and whatnot utilizes the public dir.

I also don't really want to have my remote and local out of sync. My desired workflow:

code locally     <---
     |               |
npm run dev/watch  --
     |
npm run prod
     |
push to GitHub
     |
deploy to host
     |
enjoy fresh website without any changes to remote files on host :)

hannebaumsaway
  • 2,644
  • 7
  • 27
  • 37

2 Answers2

0

You don't want to move the contents of public to the root and serve your app from the root because you will expose important config files such as .env.

Create another .htaccess in your root with the following contents:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

Which will rewrite all of your urls to the public directory. This should work in the local and production environment.

Andrew
  • 1,745
  • 1
  • 21
  • 29
  • I wouldn't be serving from root, though, I'd be serving from `public_html`. Does that change your answer? And my root already has an .htaccess file; would I add your code to it? Finally, is this assuming my folder structure above is ALL nested under the host's `public_html` folder? (So there would be a `public` folder *under* the `public_html` folder?) – hannebaumsaway Feb 01 '21 at 18:02
  • Your project's root should be located directly in ```public_html``` as I think you have described above. You essentially do not want your ```index.php``` to be located in your project's root (or ```public_html``` in this case). Your ```index.php``` should be located in ```public_html```->```public```. You will have a ```.htaccess``` directly in ```public_html``` with the contents I listed above in the answer, you will also have another ```.htaccess``` file in ```public_html```->```public``` where your ```index.php``` file is that is what was created with your project (I have not modified mine) – Andrew Feb 01 '21 at 18:21
  • Now my root folder (locally) seems to be Forbidden (403) after creating that .htaccess file.... – hannebaumsaway Feb 01 '21 at 19:51
  • Is it working Not sure exactly what you mean by your root folder is Forbidden, you would not want to be able to access the files in your root publicly. Do you mean when you try to access the index of your site? Can you access any pages? – Andrew Feb 02 '21 at 01:17
0

Have you tried to change public path in Laravel : See answer here maybe this is a solution.

Frédéric Klee
  • 145
  • 1
  • 9