1

I've set up my VPS server and am trying to set up a Laravel project. I have a /public_html/ folder with an index.html that's shown when visiting the website URL.

  • I have a laravel project in: /public_html/project_one/
  • The original index.php is located in /public_html/project_one/public/index.php

I want the website to show to Laravel project_one by default, but since yesterday I literally googled this for 4 hours, I tried every solution on stackoverflow, nothing worked... I think I've tried over 10 versions of .htaccess I could find.

The question:

Does anyone know how to setup the .htaccess inside /public_html/ so it redirects to my Laravel project?

I though I needed to upload my Laravel folder in: /public_html/project_one/ but is there a better location?

I want the main url to show the Laravel project. So www.mainurl.com is Laravel's project_one/public/index.php file. I do not want /project_one/ to be written in the URL!

Just out of curiosity, if I now visit: www.mainurl.com/project_one I get a 403 forbidden.

Failed solutions

In /public_html/ I tried to add .htaccess with:

DirectoryIndex index.php

#Redirect to /project_one/public if you haven't already and the IP is okay
RewriteCond %{REMOTE_ADDR} ^1\.2\.3\.4*
RewriteCond %{REQUEST_URI} !^/project_one/public
RewriteRule ^(/lucaphoto)?(.+) /project_one/public$2 [L]

#if IP does not match and you ARE in the folder, then Redirect to root
RewriteCond %{REMOTE_ADDR} !^1\.2\.3\.4*
RewriteCond %{REQUEST_URI} ^/project_one/public
RewriteRule .? / [R=301,L]

This gives me something really strange:

  1. On Chrome: it redirects me to www.mainurl.com/project_one -> And then gives me a 403 forbidden error.**
  2. On safari: it redirects me nowhere but gives me this error:

Forbidden. You don't have permission to access / on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

Laravel's original .htaccess File

By the way, Laravel has an original .htaccess file at /project_one/public/.htaccess. And it looks like this:

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

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
mesqueeb
  • 5,277
  • 5
  • 44
  • 77
  • Do you have access to modify / create Apache configurations directly? – David Barker Jul 01 '16 at 05:43
  • Yes I think so. It's a VPS (Virtual Private Server) so I have root access. I'm still new to setting up servers though... I only worked on shared servers until now. – mesqueeb Jul 01 '16 at 05:44
  • @DavidBarker Do you think I could solve it with Apache configurations? – mesqueeb Jul 01 '16 at 08:16
  • If your VPS user has permission to SSH in and create symlinks, you are doing this wrong. You are jumping through hoops that only a shared hosting situation would require. – Qevo Aug 09 '16 at 23:38

1 Answers1

0

The actual laravel application ( anything but the public folder ) is not supposed to be public and therefore should not be placed inside the public ( public_html ) folder.

I actually tried severel things to get something similar to your setup running. My conclusion was - that only changing the .htaccess file will net e enough to run laravel inside a public folder.

The most easy solution to quickly setup a laravel application is to use a subdomain which directly points to the public folder.

www.mainurl.com /* points to .../public_html/ */
laravel.mainurl.com /* points to .../laravel/public/ OR ( but not good ) */
/* points to .../public_html/my-project/public/ */

What you actually will need to do then is to change the root of your domain / subdomain to point into another folder - no changes to .htaccess are needed then.

Update

If your site is www.example.com and you want your laravel app to show when opening this site you have two options.

  1. Put the content of laravels public folder to public html and change the references to fit the public_html instead of public folder.

  2. If your Filestructure on the server is like .../var/stuff/.../public_html/ then upload everything to ../var/stuff/my-project and then go to your hosters interface and make an internal redirect to the ../var/stuff/my-project/public folder and everything will be fine without chaning anything in your laravel code. ( anything but .env )

So the actual problem is that your domain points to the folder public_html, instead of the public folder of your app. However please dont put your app into public_html directly, put it parallell to it and change where the domain points to.

if you give us some information about your hoster we might can provide a guide

Ismael Padilla
  • 5,246
  • 4
  • 23
  • 35
Frnak
  • 6,601
  • 5
  • 34
  • 67
  • Thank you very much! I'm not quite certain what you mean though... Basically I have a laravel project: e.g. `project_one` on my local machine, and I just bought and setup my first VPS. I was able to install everything through [Vesta](https://vestacp.com/), and my main domain goes to `/public_html/index.html`. Now I want to upload my Laravel project and have the main url go to the laravel project. → That's why I thought to just upload the laravel project into the `/public_html/`, but are you saying that this is not the correct location? Where instead should I upload my Laravel project? Thanks! – mesqueeb Jul 01 '16 at 05:37
  • I can always upload my Laravel project in a different folder, no problem for me! As long as the main URL would bring me there. – mesqueeb Jul 01 '16 at 05:41
  • Dear Frank, Thank you very much. My hoster is a Japanese company called http://onamae.com So I doubt you'd be able to find any guides. My current public folder is in `/mainurl.com/public_html/` So I will try and put the Laravel folder also in the `/mainurl.com/` folder. Then I'll google how to make the redirect! Sounds difficult. But then again, compared to programming, everything about servers is difficult. : ) – mesqueeb Jul 01 '16 at 05:49
  • It really depends on your hoster if this is difficult or not. For it was opening my webinterface ( they provide ) and chosing the folder which should be my root. Other hosters might require to connecti via ssh and do it via console – Frnak Jul 01 '16 at 05:57
  • In addition I suggest you just ask your hoster directly "how to change the public folder" – Frnak Jul 01 '16 at 05:57
  • Dear Frank, my control panel only has DNS PTR settings and SSH connection information. That's it... (it's only 8 usd per month so that's why it's cheap I guess...) I'll try asking them through email! – mesqueeb Jul 01 '16 at 06:01
  • Dear Frank, I tried to move all Laravel files from `/public_html/project_one/` to `/public_html/` and delete the project_one folder. Then I also deleted the original index.html that was present on the server. However, this still doesn't work. It gives me: >Forbidden – mesqueeb Jul 01 '16 at 08:57
  • only moving it to public_html will not work. Laravel expects the folder to be named public, chaning the name of the folder will also not work since your webserver expects it to be called public_html. Either one of these will need to be changed. Also don't forget to set 777 to storage folder etc. – Frnak Jul 01 '16 at 09:00
  • I got all contents of the Laravel "project one" into the "public_html" folder. That means that now my index file lies here: `/public_html/public/index.php`. On my own computer this gets hosted properly through "php artisan server" So I wonder what I could still change. Laravel's folder order is the same, but do I need to put the `/public/index.php` file into the public_html folder? – mesqueeb Jul 01 '16 at 09:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/116167/discussion-between-frank-provost-and-mesqueeb). – Frnak Jul 01 '16 at 09:07