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

MooseSaid's avatar

Subdomain routing not working and working at the same time

I'm working on a Larave, Inertiajs and Vue project where I want every product to open on a route like this https://{user:username}.example.test/{product:slug}

So basically I need my website to run on example.test and any product to show on username.example.test/productSlug

I had an issue where I'm getting CORS errors and I wrote a thread here https://laracasts.com/discuss/channels/laravel/redirects-not-working-no-access-control-allow-origin-header-laravel-inertiajs-and-vue?page=1&replyId=810625

Unfortunately I didn't get the help I needed here but after hours of searching I found this on stackoverflow which have helped me to finally succeed on making the redirect to the correct URL works but the problem is that It doesn't really work.

To explain this:

The controller store action finishes what It's supposed to do and then redirects me to a working page of my product without cors errors, the only problem is that it shows in the browser as example.test/productSlug while it's supposed to be username.example.test/productSlug. When I refresh the page when I'm on that incorrect url It gets me page not found error which make sense because the actual correct route contains my desired page.

When I tracked the request on firefox I could see that the host is correct (username.example.test) but the url I'm reaching is 'example.test', also when I'm redirected to that incorrect route I could see my product and everything works just fine but it's not a working url because when I refresh It gives me page not found and when I manually write the correct url username.example.test/productSlug It directs me to a working page of my product.

I hope you're not confused.

This is the redirect line in my ProductsController@store:

public function store(StoreProductRequest $request)
    {
		
		// Code
		
        return redirect()->route('products.show', [$user, $product]);
    }

This is how my route looks like:

Route::domain('{user:username}.' . env('APP_URL'))->group(function () {

    Route::get('{product:slug}', [ProductController::class, 'show'])->name('products.show');
    
});

And this is how I show the product:

    public function show(User $user, Product $product)
    {
        return Inertia::render('Products/Show', [
            'user' => $user,
            'product' => $product,
            'thumbnails' => $product->productimages
        ]);
    }

I made those changes to cors.php:

    'paths' => ['api/*', '*', 'sanctum/csrf-cookie'],

    'allowed_methods' => ['*'],

    'allowed_origins' => ['*'],

    'allowed_origins_patterns' => [],

    'allowed_headers' => ['*'],

    'exposed_headers' => ['x-inertia'],

    'max_age' => 0,

    'supports_credentials' => false,
0 likes
8 replies
bobbybouwmann's avatar

The only thing I can think of is that the user is missing somehow somewhere in the request.

Have you tried not using RouteModelBinding and instead just a string? So replace it with

domain('{username}.' . env('APP_URL'))

Then you need to convert the username yourself to a model, but just to make sure RouteModelBinding is not the issue!

1 like
MooseSaid's avatar

@isimmons I tried that, didn't work as well, still I'm being redirected to example.test/slug and the username is not included to the url I'm redirected to. If I refresh the page it doesn't work anymore and if I manually head to username.example.test/slug It's the working url that I need to be redirected to. I'm going crazy!

Here is what I did to follow your solution:

Route:

Route::domain('{username}.' . env('SESSION_DOMAIN'))->group(function () {
    Route::get('{slug}', [ProductController::class, 'show'])->name('products.show');
});

ProductController@store :

        $username= $user->username;
        $slug = $product->slug;

        return redirect()->route('products.show', [$username, $slug]);
    }

ProductController@show :

    public function show($username, $slug)
    {

        $user= User::where('username', $username)->first();
        $product = Product::where('slug', $slug)->first();

        return  Inertia::render('Products/Show', [
            'user' => $user,
            'product' => $product,
            'thumbnails' => $product->productimages
        ]);
    }

This redirects me to a working page but incorrect url, which is without the subdomain and if i visit the desired url I find it exists and working.

mix5003's avatar

i think you can not use '{user:username}.' . env('APP_URL') if i remember correctly APP_URL is not just a domain, it fully URL (eg. http://example.com/ or http://www.example.com/) i think you may try this options

  1. create new env from domain suffix
  2. try '{user:username}.' .parse_url(env('APP_URL'))['host'] in domain

but i prefer options 1, in case APP_URL may base www. prefix it will not mess up your subdomain

1 like
MooseSaid's avatar

@mix5003 Did that, I added a new env calles SESSION_DOMAIN=example.test domain('{subdomain}.' . env('SESSION_DOMAIN')) still getting redirected to a url without the subdomain. If I refresh the page it doesn't work anymore (because the url is incorrect) and if I visit username.example.test/slug I find my desired page working on the intended url.

MooseSaid's avatar

This is driving me crazy! Subdomains isn't working even if I hard set the redirect like this redirect()->to('http://username.example.test/productSlug') it gives me in the browser url example.test/productSlug only! I even used Inertia to redirect from the frontend to a url and did the same!

Please or to participate in this conversation.