I am trying to set up a rewrite rule that will redirect a subdomain only if the root directory is requested. I am attempting to set these up as global rules that will apply to all my domains so that I do not need to configure this each time I set up a new domain. To clarify why that is of a concern, well because I am using cPanel which will do a lot of automatic things for any new domain/account...
To explain, my desire was to redirect all www, mail, and mta-sts subdomains, as well as any www or mail subdomains to subdomains (www.subdomain.domain.tld -> subdomain.domain.tld.) Specifically for mta-sts, I can't have requests going to the necessary file at that subdomain being redirected. I have tried a number of ways to clarify only to redirect requests made to the root, but I am having no luck.
I was successful doing most of what I wanted with the following:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.+?)\.(.+?)$ [NC,OR]
RewriteCond %{HTTP_HOST} ^mail\.(.+?)\.(.+?)$ [NC,OR]
RewriteCond %{HTTP_HOST} ^mta-sts\.(.+?)\.(.+?)$ [NC]
RewriteRule ^/?(.*)$ http://%1.%2/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.(.+?)\.(.+?)\.(.+?)$ [NC,OR]
RewriteCond %{HTTP_HOST} ^mail\.(.+?)\.(.+?)\.(.+?)$ [NC]
RewriteRule ^/?(.*)$ http://%1.%2.%3/$1 [L,R=301]
Appreciate any feedback.
Thanks CBroe for the tips. Here is the final result, much cleaner.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mta-sts\.(.+?)\.(.+?)$ [NC]
RewriteRule ^/$ http://%1.%2/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(www|mail)\.(.+?)\.(.+?)$ [NC]
RewriteRule ^/?(.*)$ http://%2.%3/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(www|mail)\.(.+?)\.(.+?)\.(.+?)$ [NC]
RewriteRule ^/?(.*)$ http://%2.%3.%4/$1 [L,R=301]