I am trying to redirect the URL to the corresponding subdomain.

For example:




I have tried:

Redirect /account/user http://user.example.com

but this shows the homepage of example.com instead of the content of example.com/account/user.

At the domain level, I set * as an alias of example.com.

  • "At the domain level, I set * as an alias of example.com." - What exactly do you mean by that? Did you create a "wildcard" CNAME record in the DNS? Did you set the ServerAlias appropriately? Anything else? How does user.example.com route to example.com/account/user? It sounds like you are perhaps trying to create the redirect before you've established how the URL routes to the correct place? (Which URL are you linking to?) – MrWhite Aug 28 '18 at 14:01

Your redirect works as expected. You didn't configure anything that would show something different at http://user.example.com, so you should that happen?

Why do you even want to do this? Normally you would redirect http://user.example.com to something like http://example.com/account/user. Do you want to change your Apache configuration for each new user? Use a script that evaluates the Host header sent by the client?

I think what you want is something like

RewriteCond "%{HTTP_HOST}"   "!^www\.example\.com" [NC]
RewriteCond "%{HTTP_HOST}"   "!^example\.com" [NC]
RewriteRule "^/?(.*)"        "http://www.example.com/account/%{HTTP_HOST}/$1" [L,R,NE]

What you say you want is achieved by

RewriteRule /account/(.*)     "http://$1.example.com/"

But I think what you really want is exactly the reverse.

  • i want that every user to have his own subdomain.. redirect url to subdomain and show the content of that url on the subdomain.. rewrite url to subdomain ... is that even possible ? thank you – Lolak Aug 19 '18 at 12:49
  • It is possible, from the description you already did it. The open point is who to display the content you or the users want at that address. There are different possibilities, I mentioned some already. – RalfFriedl Aug 19 '18 at 12:55
  • sorry i am lost, is it possible using .htaccess to accomplish that ? example.com/account/user should point to user.example.com.. user.example.com should display what originally displayed on example.com/account/user – Lolak Aug 19 '18 at 13:01
  • I think you want something like in the example. You may want to strip the example.com from the HTTP_HOST. – RalfFriedl Aug 19 '18 at 13:15
  • thank you but this not working... we can rewrite the url of example.com/account/user1 to example.com/account/hellouser1 .. instead of rewriting it to example.com/account/hellouser1 i want to be hellouser1.example.com – Lolak Aug 19 '18 at 13:21

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.