(NOW FIXED)

#1
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.

Thanks,

Ryder Cragie
 
#2
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.
 

serpent_driver

Well-Known Member
#6
Try this:

Code:
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.
 
#9
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.
 

serpent_driver

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

Code:
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
 
#13
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.
 

serpent_driver

Well-Known Member
#14
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.
 
#15
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:
#16
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.
 

serpent_driver

Well-Known Member
#17
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.
 
Top