Send all requests to another server

Posted 1 year ago by acupofcode

Hey guys, I need to redirect all requests on production to another server for authentification reasons.. For now I'm using a middleware that does the redirection. Is there any other way that might be better?

Let's say I have a menu item that links to http://localhost:8080/page2 that request actually should go to http://localhost:1234/page2. This server checks if the user is logged in and redirects the request back to http://localhost:8080/page2. I need to make sure that EVERY request goes to http://localhost:1234 on production, but only if env('APP_ENV') is prod.

So I wrote the following middleware and added it to my route's middleware which works fine.

<?php

namespace App\Http\Middleware;

use Closure;

class RequestRedirect
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if(env('APP_ENV') == 'prod' && !str_contains($request->url(), env('REQUEST_URL'))) {
            return redirect(env('REQUEST_URL', '') . $request->path());
        }
        
        return $next($request);
    }
}

Is there another, maybe better, way to handle it? I was thinking about generating the links on e.g. menu items to be http://localhost:1234/page2 so the request actually goes directly to the other server instead of being redirected from my application?

Any solutions, hints, tips, ideas are welcome! =)

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.