1

I have:

subdomainaaa.mydomain.com
subdomainaaa.mydomain.com/anything/whatever
subdomainbbb.mydomain.com
subdomainbbb.mydomain.com/anything/whatever
...
...
anysubdomain.mydomain.com ...

Practically wild card subdomain.

And I need rewrite rule to make it all:

www.mydoamin.com/and_whatever_goes_afterwords

Basically, how to redirect zillion subdomains to www, leaving anything that goes after .com/ as it is.

I must do some sort of permanent redirection because I can't lose old links, just to redirect them to new ones with www instead of subdomains

Please, I was looking all over the internet for this, and I get only chunks of regex which I don't know how to put together, and whatever I mix in my .htaccess file - I get server error. Or, I manage to redirect anything just to the homepage www.mydomain.com. I hope I was clear. I am a regex newbie (wannabe), so, please give me the whole chunk to put in .htaccess. Or, if you think I am doing something wrong - give me suggestion how to accomplish my goal. Thanks a lot guys!!

1 Answers1

2

You may use this rule in your site root .htaccess:

RewriteEngine On

# add www and keep same URI
RewriteCond %{HTTP_HOST} ^(?!www\.)[^.]+\.(mydomain\.com)$ [NC]
RewriteRule ^ http://www.%1%{REQUEST_URI} [R=301,L,NE]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Thanks! But, this needs a little change, please. This makes 'subdomain.mydomain.com/something' become 'www.subdomain.mydomain.com' and **no /something**, so it needs 2 modifications to be - 'www.mydomain.com/something' without .subdomain. in between www and mydomain. Thanks a lot @anubhava!! – Mirjana Katalina Feb 23 '18 at 19:07
  • This adds www in front of whole url, I need www to replace subdomain: this makes sub.domain.com became www.sub.domain.com - and I need www.domain.com. I used new browser, cleared cache and all, put it on the top of htaccess. Thanks @anubhava – Mirjana Katalina Feb 23 '18 at 20:16
  • 1
    Great! Works as I wanted! Thank you very much @anubhava! – Mirjana Katalina Feb 23 '18 at 20:32