简体   繁体   中英

Laravel - Subdomain route prefixes causes other routes to not return “Not found”

[NOTE] - This issue was found to be due to incorrect Apache configuration. I have posted an answer which explains what I did wrong. I hope this helps!


I'm starting a project with Laravel and I've begun with creating the general routes. I want to create a request structure so that URLs of example.com go to organic routes, admin.example.com go to the admin dashboard of my app, and {workspace}.example.com goes to a dynamic route for my users' personal workspaces.

Currently I have the following routing structure:

Route::group(array('domain' => 'admin.example.com'), function() {

    Route::get('/', function() {
        echo 'This route works fine';
    });

    Route::get('/test', function() {
        // This route doesn't work...
        echo 'Test';
    });

});


Route::group(array('domain' => '{workspace}.example.com'), function() {

    Route::get('/', function() {
        echo 'This route works fine';
    });

    Route::get('/test', function() {
        // This route doesn't work...
        echo 'Test';
    });

});


Route::group(array('domain' => 'example.com'), function() {

    Route::get('/', function() {
        echo 'This route works fine';
    });

    Route::get('/test', function() {
        // This route doesn't work...
        echo 'Test';
    });

});

As you see, I have three routing groups. The first is for the admin prefixed URLs (to ensure that admin.example.com is not mistaken for a workspace prefix). There is the workspace prefix, and then finally the no-prefix route group.

As indicated in the code, all Route::get('/', ... routes work fine, however anything that is not the root does not work. I am receiving 404 errors when trying to go to example.com/test (and all the other domain prefixes).

What can I do to my routing to fix this and get the routes Route::get('/test', ... to work rather than just the / routes?

Much appreciated!

This issue was being caused by the Apache configuration being incorrect rather than the routing being incorrect.

To avoid having issues like this in future, ensure that AllowOverride is set to All in your project's <Directory /var/www/myProject/public> block the apache.conf file like below:

<Directory /var/www/myProject/public>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

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