Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ThoHC's avatar
Level 1

Deploy laravel in subfolder of wordpress 404 not found

Hi, my customer's server is sharehosting and run wordpress. But I need deploy Laravel 9 web to subfolder. This is tree folder:

/site.com/pubic_html/{sourcecode wp}
/site.com/pubic_html/lp/{laravel source}

I need to run laravel in URL: site.com/lp

My htaccess of laravel root folder: /site.com/pubic_html/lp/.htaccess

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

But site.com/lp always show 404 not found (page 404 with style laravel)

Thank for your help

0 likes
34 replies
gych's avatar

Did you add your complete Laravel project to the lp folder or only the files from the public folder?

ThoHC's avatar
Level 1

@gych I install complete laravel project by composer, all file source code of laravel in lp folder

ThoHC's avatar
Level 1

@gych I was try clone laravel project to another folder, same level with public_html, and only copy all file in public folder to lp folder, custom path in index.php. but it show 405 error page. error page of sharehosting server

gych's avatar

@ThoHC Ok your laravel project folder should be added outside of public_html

Do you have SSH access on the shared host?

1 like
gych's avatar

@ThoHC Ok good

Put the Laravel project folder on the same level as the public_html folder (not inside public_html) If the lp folder already exists, delete it first, otherwise you will get an error when trying to create a symlink.

Now create the symlink between the laravel project's public folder and the lp folder via ssh terminal. When the terminal is located inside the laravel project folder run this command.

ln -sr public ../public_html/lp

This will create the lp folder inside public_html with a symlink between the laravel project's public folder and the lp folder.

1 like
ThoHC's avatar
Level 1

@gych Thank for suggesting. I created symlink, and web show 405 Method Not Allowed when connect to site.com/lp. error from server, not laravel

gych's avatar

@ThoHC Ok and your htaccess files is it like the original files or changed? When I once did this I didn't had to change anything to the htaccess files

ThoHC's avatar
Level 1

@gych this is some htaccess file:

/lp/.htaccess

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

/lp/public/.htaccess

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

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

/public_html/.htaccess

SetEnvIf Request_URI ".*" Ngx_Cache_NoCacheMode=off
SetEnvIf Request_URI ".*" Ngx_Cache_AllCacheMode

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-di

RewriteRule . /index.php [L]
</IfModule>

# END WordPress

gych's avatar

@ThoHC Delete this /lp/.htaccess file, its not necessary. Then test again to see if you still get the error.

ThoHC's avatar
Level 1

@gych thanks for keep support for me. I was deleted /lp/.htaccess but web still 405 error

gych's avatar

@ThoHC No problem :) Did you change anything to the default index.php in your laravel application's public folder?

ThoHC's avatar
Level 1

@gych Nothing to change index.php , I think 405 error from server is mean can't connect to outside of public_html.

When laravel project push into public_html, web show 404 notfound page of laravel. I try add dd(1) to index.php and web show dump:

$kernel = $app->make(Kernel::class);
dd(1); // web show dump
$response = $kernel->handle(
    $request = Request::capture()
)->send();
dd(1); // web show 404 not found page
$kernel->terminate($request, $response);
gych's avatar

@ThoHC If your routes are cached try to run php artisan route:clear

Does your host use nginx or apache?

ThoHC's avatar
Level 1

@gych I'm try clear all cache but still error. My shared hosting use nginx

ThoHC's avatar
Level 1

@gych Hi, I have some change and loaded source laravel, but it not correctly. For now, my directory:

  • /lp_source (laravel project, all file)
  • /public_html (wp project)

I marked symlink:

ln -sr lp_source/public public_html/lp

When load site.com/lp show blank screen. after checklog I see web load js or CSS file from site.com/build/..., this must be site.com/lp/build/...

I don't know how to config htaccess to make load build from site.com/lp, please help.

I try make symlink build folder to public_html

ln -sr lp_source/public/build public_html/build

and then link URL auto duplicate path from site.com/lp to site.com/lp/lp/ and load success web lp, but when F5 or ctrl+R link site.com/lp/lp/ web will show 403 page because not have lp folder in lp folder

gych's avatar

@ThoHC So you are now trying to test if it works with a fresh Laravel installation?

What is the APP_URL in the env file ? Is it still https://site.com/lp ?

ThoHC's avatar
Level 1

@gych APP_URL just https ://site. com, change to https ://site. com/lp same error

ThoHC's avatar
Level 1

@gych for now I don't know why URL auto duplicate path. you can visit mypage to view behavior https ://n-kaihatsu. net/lp2

gych's avatar

@ThoHC Did you change anything to htaccess files or are they still as default?

ThoHC's avatar
Level 1

@gych htaccess still as default in /lp_source/public/.htaccess and public_html/.htaccess

gych's avatar

@ThoHC Ok and you still have symlink between lp_source/public and public_html/lp ?

ThoHC's avatar
Level 1

@gych yes. I have 2 symlink

lp_source/public and public_html/lp

and

lp_source/public and public_html/lp2

because /lp have error and cache from server, so I testing with /lp2

gych's avatar

@ThoHC

You can't disable the caching for that directory ?

Something went wrong when setting this up, my first guess would be the htaccess file but since you did not change anyting to that.

You also made multiple symlinks try to delete the symlinks and directories lp and lp2

Then create a symlink only between the public folder and lp2

1 like
ThoHC's avatar
Level 1

@gych Server was remove caching, url lp is worked, but still duplicate lp when I connect https://n-kaihatsu.net/lp, web auto add /lp => https://n-kaihatsu.net/lp/lp/, look like only add to url without redirect reload.

this doesn't happen at my localhost

gych's avatar
gych
Best Answer
Level 29

@ThoHC Try to add this rule in your laravel project's htaccess file

RewriteRule ^lp/lp/([^/]+)/?$ /lp/ [R=301,L]
1 like
ThoHC's avatar
Level 1

@gych I use window.history.pushState to remove lp in URL, same idea, but it's not as good as your way, anyway it worked, thank you very much

1 like
puklipo's avatar

Don't use Laravel in subfolder.

1 like
Tray2's avatar

Why are you complicating things. Just get another webspace for your Laravel application and create a subdomain.

example.com for the wp

lp.example.com for laravel.

1 like
ThoHC's avatar
Level 1

@Tray2 of course, all will easy if use subdomain. But my customer wants it's example.com/lp. They are very conservative

Please or to participate in this conversation.