Vitor's avatar
Level 4

Wordpress with Laravel on subfolder

I need to add a new Laravel app to an existing Wordpress website. Both will run via Apache webserver. The idea is that:

In the filesystem, the root folder contains the Wordpress files and a subfolder laravelapp contains the Laravel files (so, the laravel public folder is a subfolder of laravelapp).

When I browse these, this is the current returned result:

This shows me that mod_rewrite is available and working. But I want users to be able to just use the https://website.com/laravelapp to access that laravel app (not the public folder).

I've been trying to setup the .htaccess to make this arrangement possible, but am getting stuck. I currently have:

  • .htaccess on root of website
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/laravelapp [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
  • .htaccess on laravelapp folder
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /laravelapp/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
  • .htaccess on laravelapp/public folder contains the standard laravel .htaccess file from the distributed package.

Is there any way to make this possible? I'd really appreciate some hints.

0 likes
13 replies
Vitor's avatar
Level 4

[edit: this here explained didn't work after all, exposes key root files such as the .env, see below]

Wow... hours after trying many options, and within less then 20m after publishing my question I find the answer elsewhere...

For future reference of anyone seeing this, the answer is to have the /laravelapp/.htaccess with the following:

DirectoryIndex public/index.php
<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 ^ public/index.php [L]

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

It just worked like this! :)

2 likes
bashy's avatar

@Vitor Assets still work and everything? Can't view .env or composer.json files?

Vitor's avatar
Level 4

@bashy Good point! Thanks for bringing that to my attention! :)

.env and composer.json are still available when requested in the browser. That's a no-no of course.

Any help how I can make sure this doesn't happen?

bashy's avatar

@Vitor It's always best not to expose the Laravel root files at all to the browser. If .htaccess stops working one day, your credentials are gone.

I would actually opt for an alias (symlink). This is much easier with nginx imho but I wouldn't be able to help much with Apache since the syntax makes me feel sick :)

Vitor's avatar
Level 4

Thanks anyway @bashy. I agree, Laravel root files should never be accessed.

Anyone else would know how to achieve this? Thanks in advance!

tarik's avatar

I'm sorry If I give you the wrong answer. So some one please correct me. I did just for test this. For example. https://website.com/ -> Wordpress website https://website.com/laravelapp -> Laravel web application

in your root https://website.com/laravelapp make index.php that redirects to public folder.

header("Location: public/index.php");

That worked for me. Or you can follow the simplier best answer :

http://stackoverflow.com/questions/28364496/laravel-5-remove-public-from-url#answer-28735930

Vitor's avatar
Vitor
OP
Best Answer
Level 4

So, after coming back to this issue, I think I found how to achieve this (please @bashy would really appreciate your feedback, very valuable so far).

Besides all the setup i described in my question, i only changed the .htaccess of the laravelapp/ folder to:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

This seems to work, including not returning files such as .env and composer.json in the laravelapp/ ... I will try routes and assets as well and post here the outcome.

4 likes
bashy's avatar

If you can't view root files and everything directs to public folder, sounds good to me. I don't know Apache well enough, only nginx.

Vitor's avatar
Level 4

Yeah, I wish I could have the client change hosting and use nginx, would be so much easier. I dislike .htaccess setup! Thanks for the feedback!

In the meantime I confirmed this is now solved :D

Please or to participate in this conversation.