skip to Main Content

What is the proper way to add a subdomain into your routes? I have a laravel/homestead project working on my local computer but when I move it to HostGator, a shared host server, it no longer works. Well the home page works but the links to the sub-pages don’t.

For example, the following route works fine on my local version:

Route::get('/appointments', 'AppointmentController@index');

But if I use that route on the remote server, it takes it to tekknow.net/appointments instead of tekknow.net/medaverter/appointments.

So I followed instructions from here:
https://laravel.com/docs/5.6/routing#route-group-sub-domain-routing
and added a prefix like this:

Route::prefix('MedAverter')->group(function() {
  Route::get('/appointments', 'AppointmentController@index');
});

But that doesn’t work either. It also goes to /tekknow.net/appointments

I also tried changing the route to this:

Route::get('/MedAverter/appointments', 'AppointmentController@index');

But that also went to tekknow.net/appointments

Anybody know what I’m doing wrong?

EDIT: I went onto my HostGator cPanel and looked at all my subdomains and saw that my subdomain root was medaverter.tekknow.net which is linked to Document root of medaverter.tekknow.net/MedAverter which gets redirected to http://www.tekknow.net/MedAverter. So I renamed my folder from medaverter to MedAverter to match the subdomain redirection.

Here is a screenshot showing what I see in cPanel for columns Subdomains.Root Domain, Document Root, and Redirection
enter image description here

3

Answers


  1. When you try php artisan route:list | grep appointments,

    it prints:

    [~/www/MedAverter]# php artisan route:list | grep appointments 
    
    | | GET|HEAD | MedAverter/appointments | | AppHttpControllersAppointmentController@index | web | 
    

    That means your route MedAverter/appointments is working for laravel.

    Error 404 means route cannot be found.

    So I think that’s something wrong with your nginx configuration.

    When I try http://tekknow.net/MedAverter/MedAverter/appointments.
    It has really found the route with error 500.
    medaverter-500

    So, I think you have defined this code in your nginx configuration:

      location = / {
        rewrite ^ https://tekknow.net/MedAverter;
      }
    
    

    Solution 1:

    Change back to rewrite ^ https://tekknow.net/; in nginx configuration.

    (I’m not familiar with hostGatar cPanel, but I think you can change medaverter.tekknow.net/MedAverter redirected to http://www.tekknow.net/`)

    And in your laravel project, you need to keep prefix MedAverter to appointments.

    Solution 2:

    Keep the rewrite code. It means you don’t need to change the cPanel redirect.

    And remove prefix MedAverter in laravel routes. HostGatar(Nginx) will automatically redirect with this prefix MedAverter for appointments.

    Login or Signup to reply.
  2. Clear all caches

    The problem you’re getting can be due to cache. So, make sure that’s not the problem by running

    php artisan route:clear && php artisan view:clear && php artisan config:clear && php artisan cache:clear && php artisan clear-compiled
    

    Base URL

    If you want to change the base URL of your Laravel app, that’s something already asked.

    Multiple Path Prefixes

    In this case you have a more than one route with the same prefix in their path. If that’s the case, you can use route groups to simplify the structure.

    Route::prefix('medaverter')->group(function () {
        Route::get('/', function () {
            // Path /medaverter
        });
        Route::get('appointments', function () {
            // Path /medaverter/appointments
        });
    });
    

    This would go to, respectively, tekknow.net/medaverter and tekknow.net/medaverter/appointments. You could also create another prefix, like testing, configure in the same way and go to tekknow.net/testing or tekknow.net/testing/whatever.

    Namespace prefixes

    When you’re grouping routes by route prefix, it’s likely their controllers have a similar PHP namespace. In the medaverter example, all of the medaverter routes’ controllers might be under a Medaverter namespace.

    Route::namespace('Medaverter')->group(function () {
        // AppHttpControllersMedaverterAppointmentController
        Route::get('medaverter/appointments', 'AppointmentController@index');
    });
    
    Login or Signup to reply.
  3. u can use route groups for that here is a sample try this:

    Route::group(['prefix' => 'medaverter'], function () {
            Route::get('/appointments', [AppointmentController::class, 'index'])->name('index');
         
    });
    

    or

    Route::group(['prefix' => 'medaverter'], function () {
        Route::get('/appointments', 'AppointmentController@index');
     
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search