5

I'm trying to deploy laravel 5.5 inside a WordPress sub-directory on a shared hosting. I tried too many suggestions from google but I still can't make it work. It always returns a 404 error. WordPress is installed inside /public_html and I need to install Laravel on /public_html/my-laravel-app Here's what my .htaccess files look:

/public_html/.htaccess

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

# protect wpconfig.php
<files wp-config.php>
order allow,deny
deny from all
</files>

# protect the htaccess file
<files .htaccess>
order allow,deny
deny from all
</files>

# disable the server signature
ServerSignature Off

/public_html/my-laravel-app/.htaccess

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteBase /my-laravel-app/
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule . /my-laravel-app/public/index.php [L]
</IfModule>

/public_html/my-laravel-app/public/.htaccess

<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>

/public_html/my-laravel-app/public/index.php (some part of index.php)

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

/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/

$app = require_once __DIR__.'/../bootstrap/app.php';
Dexter Bengil
  • 5,995
  • 6
  • 35
  • 54
  • Where is the `.htaccess` file ?! – Maraboc Oct 23 '17 at 13:10
  • @dexterb Make sure, Is laravel working on sheared hosting, If yes then go with sub-domain. I think it's not working on sheared hosting. – er.irfankhan11 Oct 23 '17 at 13:10
  • So when you access /my-laravel-app/public/index.php on your shared hosting, which error does it give you? When you look in the laravel.log, which errors do you see? – UnderDog Dec 20 '22 at 10:03

2 Answers2

2

I forgot to update this question, but I already solved this problem. It's not actually about WordPress' .htaccess, I get rid of my custom .htaccess settings. What I did was, create a new folder on public_html/laravel-app then inside my laravel-app, this is where I cloned the repo, so it became, public_html/laravel-app/my-app then, I copied the files(except the folders) inside my-app/public directory and moved it to laravel-app/.

$ cd /public_html/laravel-app
$ cp -a my-app/public/*.* ./  

Then I created a symbolic link for the directories inside my-app/public by running

$ ln -sf my-app/public/*/ ./

And lastly, I modified the public_html/laravel-app/index.php on line 22 and line 36,

require __DIR__.'/my-app/bootstrap/autoload.php';
$app = require_once __DIR__.'/my-app/bootstrap/app.php';

Then it's done.

Dexter Bengil
  • 5,995
  • 6
  • 35
  • 54
1

Laravel doesn't work on subfolders out of the box. You will need to tweak some files in order to get it to work. Have a look at the following article on laravel-news.com

My suggestion would be to re-consider your approach and see if there's a different way to achieve your goal that doesn't require a subfolder.

Edit:

I'd like to include more steps from the article just in case it gets removed in the future but it's just too many steps.

Nicolas
  • 884
  • 5
  • 13
  • I think it's a bit different issue from the article. I'm having trouble with the WordPress .htaccess as it keeps redirecting my subfolder to a 404. :(( – Dexter Bengil Oct 23 '17 at 13:23
  • hey @dexterb is the 404 coming from WP or Laravel? – Nicolas Oct 23 '17 at 13:32
  • 404 is coming from WP – Dexter Bengil Oct 23 '17 at 13:34
  • try excluding the laravel subdirectory from WP's .htaccess, have a look here: https://stackoverflow.com/questions/2322559/htaccess-wordpress-exclude-folder-from-rewriterule – Nicolas Oct 23 '17 at 13:55
  • Actually laravel work wherever you put it on. What if you have a VPS where you have a bounch of sites? That sites are "subfolders" and you deploy laravel inside of them, so, that is why i think your logic is not good at all – Fernando Torres May 18 '21 at 20:26
  • @FernandoTorres: nope, I think you are getting confused. When you say "a bunch of sites", sure, that will work if every site is setup to have the document root at the site folder level. That's very different from having laravel in the sub folder of a website though: that's definitely not going to work out of the box. – Nicolas May 19 '21 at 14:21