I have created a new Laravel project (v5.5) which is related to my main website. Due to SEO-technical considerations I want the link to be like this: <mainwebsite.com/laravel>
.
Both, <mainwebsite.com>
and <mainwebsite.com/laravel>
are deployed to an individual server. An Application Load Balancer redirects the traffic either to the main website or the new Laravel project.
The problem right now is that the Laravel app doesn’t know that <mainwebsite.com/laravel>
must be seen as the project’s root. (The route /
must go to <mainwebsite.com/laravel>
and not to <mainwebsite.com>
.
I’ve tried to add Route::prefix('laravel')->group() ...
to web.php, which does fix the routes, but then the app’s public dir can’t be accessed.
Using relative paths like "/css/app.css"
or "/laravel/css/app.css"
won’t fix this.
Is there a better way to set this up or does anyone know how this must be done?
2
Answers
The following did the trick for me.
Change the APP_URL in the .env file to http://www.mainwebsite.com/laravel
Move the contents of the public folder into a new folder inside the public folder and give it the same name as the path behind the URL (so in this case 'laravel').
index.php
file:require __DIR__.'/../../vendor/autoload.php';
&require_once __DIR__.'/../../bootstrap/app.php';
(add an extra/../
)webpack.mix.js
and add the following rules to the beginning of the file to make sure relative paths in your files will be rewritten to the right dir:mix.setPublicPath('public/laravel/'); mix.setResourceRoot('/laravel/');
That's it!
If you’re using apache, setup
.htaccess
to rewrite the url:This should rewrite all url paths that do not start with
laravel
to ones that do.For nginx, you could do the following: