I have hosting set up on a wildcard on my domain (*.example.com) and I would like to have the following type of redirect:

whatever-the-user-types-here.example.com > example.com/whatever-the-user-types-here

Does anyone know the code for this please? This would make my day if someone could help.


Ryder Cragie
Update: I've been through many pages of Google and I FINALLY found it! Here is the code:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com/%1 [L,R=302]

However, files are no longer accessible. So random.example.com does go to example.com/random, but the files that I am hosting are not visible. So random.example.com/test.txt does not work like it did before I added the .htaccess code. Not sure what I need to modify to get this working.


Well-Known Member
Try this:

RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301,QSA]
Place it on top of .htaccess.
If you have OLS instead of LSWS you must restart OLS to apply changes.
Well then I can't be using OLS because the changes have applied, and now I see a 404 error on my site. Your code didn't help unfortunately. Thanks anyway.


Well-Known Member
What if you only set a redirection without any condition, but that expects that each subdomain has its own document_root?

RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
Now it's just doing this again:

I tried that but then it breaks and it just stops forwarding the subdomain to my root domain, and will always redirect to example.com regardless.
Bear in mind that no matter what I am trying here, I always have "RewriteEngine On" at the top of the file.

Is there anything else we can try please? I appreciate your help.


Well-Known Member
All codes you tried from above are working codes. There is nothing special on it. If they do not work at your server something must be wrong with domain and subdomain configuration. Check if subdomains and .tld domain have same document_root.
Is document_root a directory? It might be best for me to not worry about this as I don't want to risk breaking anything server-wise.

Since that cannot be fixed with .htaccess, I would also like the following to happen, if possible please:
  1. Sub-subdomains to be turned into slashes. So ex1.ex2.example.com will go to example.com/ex2/ex1, as opposed to example.com/ex1.ex2.
  2. I will mention number 2 afterwards. Let's do it one by one.
Maybe this can be set up solely using .htaccess code?

I'd appreciate anyone's help with this as well. Thank you.
Last edited:
I apologise if you don't get .htaccess questions very often here. I just can't find any other places where this question would be relevant. StackOverflow take everything down.


Well-Known Member
As I already told you, all codes from above are working codes. If they do not work for you something must be wrong with setup for domain and subdomain configuration. Nobody can help you if this configuration unknown, but try it at StackOverflow.