skip to Main Content

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


  1. Chosen as BEST ANSWER

    The following did the trick for me.

    1. Change the APP_URL in the .env file to http://www.mainwebsite.com/laravel

    2. 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').

    /public

    --- /laravel

    ------ /css

    ------ /js

    ------ index.php

    ------ ... etc...

    1. Edit the relative paths in the index.php file:

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

    require_once __DIR__.'/../../bootstrap/app.php'; (add an extra /../)

    1. Set the right paths in 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!


  2. If you’re using apache, setup .htaccess to rewrite the url:

    <IfModule mod_rewrite.c>
       RewriteEngine on
       RewriteCond %{REQUEST_URI} !^/laravel/
       RewriteRule ^(.*)$ /laravel/$1 [L,R=301]
    </IfModule>
    

    This should rewrite all url paths that do not start with laravel to ones that do.

    For nginx, you could do the following:

    location ^~ /laravel(.*) {
      return 301 $scheme://$http_host/laravel$1$is_args$query_string;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search