简体   繁体   中英

laravel 5.5 - wildcard subdomain not working

I am upload my laravel app in the Apache shared hosting server and everything setup fine. I have CNAME record set up * subdomain and point it to the public_html/ . then I set this code in my routes/web.php :

Route::domain('{subdomain}.example.com')->group(function () {
    Route::get('/', function ($subdomain) {
        Route::get('/profiles/sub/{subdomain}', 'ProfilesController@subDomain');
    });
});

which ProfilesController@subDomain is a function to process the parameter subdomain from the URL . But the result keep sending me to the main landing page. I notice there is this error:

Uncaught (in promise) DOMException: Only secure origins are allowed

which I am not sure if it is something to do with any part.

How can I resolve this?

Update01

Following Ben's comment, I force SSL with the .htaccess below:

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

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

    # 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]
</IfModule>

<ifmodule mod_rewrite.c>
 RewriteEngine On
 RewriteCond %{HTTPS} !=on
 RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
</ifmodule>

It will force http to https . But whenever I test subdomain like xxx.example.com , Chrome will give a privacy error message, then change it back to: 在此处输入图片说明

and new error message:

An SSL certificate error occurred when fetching the script.

Just a reminder, I am trying to solve the wildcard subdomain issue here. If all this SSL is not the real reason, we can ignore that.

I think it should be

Route::domain('{subdomain}.example.com')->group(function () { 
    Route::get('/', 'ProfilesController@subDomain'); 
});

and in ProfilesController :

public function subdomain($subdomain) {
//do something
}

AND as discussed, the order of routes is important, so if you have a route declared for '/' before this logic, it will always hit it first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM