skip to Main Content

I’m trying to redirect a subdomain user to his profile.
For example: If user requested user.example.com
he gets redirected to example.com/user

I searched a lot, and most answers were old and didn’t work.

Things I have done:

  1. Created A record with *.example.com.
  2. Enabled wildcard redirect to example.com (I actually don’t know if
    this is needed) some Stack Overflow answer provided that.
  3. Created subdomain in my hosting like *.example.com (but this caused
    all my subdomains get redirected to the main landing page, which is
    example.com) some Stack Overflow answers provided that.
  4. In my larave project routes.
Route::domain('{name}'.'example.com')->group(function () {
    Route::get('/{name}', 'usersController@profile');
});
Route::group(['domain' => '{account}.example.com'], function () {
        Route::get('/{name}', 'usersController@profile');
    });

I’m really lost between old Stack Overflow answers and PHP hard code. All I want is simple. If user requested user.example.com he gets redirected to his profile example.com/user then he can navigate normally like no need for subdomain later.

I don’t know what I’m missing or doing wrong!

2

Answers


  1. Glad to know this this worked out for you. Here i am dropping a formatted answer of it. Can help others.

    RewriteEngine on 
    RewriteCond %{HTTP_HOST} ^(.+).example.com$ [NC] 
    RewriteRule (.*) example.com/%1/$1 [L,R=301,QSA]
    
    Login or Signup to reply.
  2. This worked for me.

    RewriteEngine on 
    RewriteCond %{HTTP_HOST} ^(.*).example.com 
    RewriteRule ^(.*)$ /%1/$1 [L,NC,QSA]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search