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

BigSpender's avatar

Laravel 8: redirect from another url

Good morning everyone!

I have a first domain example1.com on server. But second domain example2.com deligated to my ip address by some way (where exists example1.com). Also third domain example2.com deligated to my ip address by some way (where exists example1.com).

I need all urls should be redirected from externals domains to example1.com. By other words all what is NOT example1.com should be redirected to example1.com.

For instance http://example3.com?test=11 should be use 301 redirect to http://example1.com?test=11

I'd like to use Laravel way.

Thank you!

0 likes
6 replies
s4muel's avatar

i would do that using a .htaccess redirect rule, but if you want a laravel way, i think this should cover it.

Route::domain('example3.com')
    ->group(function () {
        Route::any('{any}', function () {
            return redirect()->away('https://www.example1.com' . request()->getRequestUri(), 301);
        });
    });

here is the .htaccess approach:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example3\.com$ [NC]
RewriteRule ^(.*)$ https://www.example1.com/ [R=301,L]
BigSpender's avatar

Thank you. I am prefer Laravel way(moreover I use nginx).

Above you added only example3.com, but I want everything which IS NOT example1.com

s4muel's avatar

sorry, this is as far as i can get:

Route::domain('{domain}.com')
    ->group(function () {
        Route::any('{any}', function ($domain) {
            if ($domain != 'example1') {
                return redirect()->away('https://www.example1.com' . request()->getRequestUri(), 301);
            }
        });

    });

the {domain} wildcard seems not to be working for whole domain names (well, makes sense, its designed to work for subdomains). so you will probably need more "groups", one for each TLD (.net, .org...)

anyway, i still would use the .htacces way, it doesnt break any laravel-way rule or something;)

i am not used to nginx, but the redirect on the nginx level is possible too (might need a bit of tweaking)

#most specific first
server {
	listen       443  ssl;
    server_name example1.com;
	#rest of the configuration
}

#more generic to match the rest
server {
	listen       80  default_server;
	listen       443  default_server ssl;
	server_name  _;
	return 301 https://www.example1.com$request_uri;
}
s4muel's avatar

but if you put more specific route first, this might actually work too, give it a try:

Route::domain('example1.com')
    ->group(function () {
        Route::get('/', function () {
			//
		});

		//put all your example1.com routes here

    });

Route::any('{any}', function ($domain) {
	return redirect()->away('https://www.example1.com' . request()->getRequestUri(), 301);
});
desaintflorent's avatar

Here what I'm using at the top of my web routes file:

$allowedDomain = parse_url(config('app.url'), PHP_URL_HOST);
$currentDomain = request()->getHost();

if ($currentDomain !== $allowedDomain) {
    Route::any('{any}', function () {
        return redirect()->away(config('app.url'). request()->getRequestUri(), 301);
    })->where('any', '.*');
}

But if you are using Laravel Forge and have alias for your domain and ssl certificates generated, you can easily edit you Nginx conf to add this at the top instead:

server {
    http2 on;
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name otherdomain.com;
    
    # FORGE SSL (DO NOT REMOVE!)
    ssl_certificate /etc/nginx/ssl/domain.com/1234567/server.crt;
    ssl_certificate_key /etc/nginx/ssl/domain.com/1234567/server.key;
    ssl_protocols TLSv1.2 TLSv1.3;

    return 301 https://domain.com$request_uri;
}

Change ssl certificate lines with your real certificate and forge will update this automatically when the certificate change

Please or to participate in this conversation.