php – Dynamic subdomain laravel: when http:// or with www. redirected to homepage-ThrowExceptions

Exception or error:

For each of our customers I create a special subdomain. When the user visits e.g. https://{company-slug}.mycompany.com it correctly shows the page.
However, when an user visits http://{company-slug}.mycompany.com and http://www.{company-slug}.mycompany.com, it will be redirected to our homepage.

My htaccess:

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

RewriteEngine On

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

And the route group in routes > web.php in Laravel:

    Route::group(
    [
        'domain' => '{subdomain}.' . config('app.domain'),
        'middleware' => 'check.subdomain',
        'as' => 'company.',
        'before' => 'force.ssl'
    ], function () {
}
How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *