0

I've a domain, like www.example.com

The main site is on FTP space visible as /example.com

I installed (copy/pasted, sigh... :( ) my Laravel app under /example.com/laravel_app.

I am able to open the laravel app, and it's fully working, if I access using full url http://www.example.com/laravel_app/public/index.php.

The problem is that, obviously, I'd like to access it using only the path http://www.example.com/laravel_app.

In the root of laravel_app, I tried to paste this .htaccess

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

While in the /example.com/laravel_app/public i'm using this `.htaccess

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

    RewriteEngine On
    RewriteBase /laravel_app/

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

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

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

So my question is NOT related the .htaccess of app's public folder, but the .htaccess for app's root folder.

How can I succesfully serve

http://www.example.com/laravel_app/public/index.php

accessing

http://www.example.com/laravel_app

?

Actually I got Laravel returning a 404

I tried this variant (seen I some question very similar to this one)

<IfModule mod_rewrite.c>
    RewriteEngine on 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^(.*)$ public/index.php/$1 [L] 
</IfModule>

But with this I got apache returning a forbidden

realtebo
  • 23,922
  • 37
  • 112
  • 189
  • Refer this https://stackoverflow.com/questions/55037835/how-can-i-use-laravel-website-in-cwp-centos-web-panel-without-public-folder-i/55038127#55038127 – Ankur Tiwari Mar 11 '19 at 09:32

1 Answers1

2

I'm assuming you have installed your application on shared hosting and ssince it's running Apache this solution should work.

Revert your .htaccess back to its original/what was deployed with laravel.

Ideally you shouldn't have your project uploaded in public_html as it exposes all the files to the web.

Move all your project files except public to a directory below public_html.

Eg.

/home/<username>
  [laravel] <-- Make this directory
  [public_ftp]
  [public_html]
  [ssl]
  etc..

Then in your public_html move the files inside your public folder to the directory you want to serve your files (address you want to go to to use your app).

/home/<username>/public_html/laravel-app
  [public_html]
    [laravel-app]
     -index.html
     -robots.php
     -.htaccess
     -[css]
     -[js]
     -[svg]
     etc..

Now edit your index.html file in the laravel-app folder and change both:

require __DIR__.'/../vendor/autoload.php';

$app = require_once __DIR__.'/../bootstrap/app.php';

..to reflect the path where you moved yoru project folder.

require __DIR__.'/../../laravel/vendor/autoload.php'; $app = require_once __DIR__.'/../../laravel/bootstrap/app.php';

Essentially in this context ../ means back one folder, so the aim is to take it back to where it will find the laravel/vendor/ folder, vice-versa.

Realistically that should sort your issue out and make for a more secure application as your .env and composer, etc. files will not be publically available without messing with your .htaccess.

Oh, also here is the default .htaccess code which should be under your public folder <project>/public:

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

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

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

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>
Tim Rowley
  • 420
  • 4
  • 15