Complicated htaccess redirect scenario

Hi folks,

I’m having a hard time figuring out the solutions for my scenario which is as follows.

I have 1 hosting account with a site that’s a website builder (built using MODx revolution).

It’s located at subdomain.domain.com
subdomain.domain.com isn’t actually a sub-domain in the sense that it has it’s own folder under domain.com. It has it’s own cPanel hosting account and it’s just parked on domain.com.

Once a site has been created using this website builder I can associate any domain those resources and it will act as a separate site.

Unfortunately, the website builder & all of it’s created sites use the same hosting space and therefore the same .htaccess file.

I have is this:
RewriteCond %{HTTP_HOST} !^$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.)$ [NC]
RewriteRule ^(.
)$ “http://www.%1/$1” [L,R=301]

Which works great for adding the “www.” to any of my created sites

BUT

It’s also adding www. to my website builder.

ie http://www.subdomain.domain.com

what I need is the website builder to stay "http://subdomain.domain.com "

but all of my created sites to append the www. prefix if it’s missing.

I’m pretty new to the mod_rewrite thing and this is making my head hurt :injured:

Any help would be greatly appreciated!

You need add another RewriteCond that says “If the host does not start with ‘subdomain’”.

I won’t tell you how it goes, but I will tell you that


RewriteCond %{HTTP_HOST} !^www\\. [NC]

means “If the host does not start with ‘www’” :wink:
(and the [NC] stands for NoCase, i.e. match case insensitive, so it will also match wWw, WWW, Www, etc).

Also, I’d replace


RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule ^(.*)$ "http://www.%1/$1" [L,R=301] 

with


RewriteRule .? http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

As it’s a lot compacter and doesn’t rely on the dreaded :redhot: (.*) :redhot: “anything” atom.