Avatar of tarrigo
tarrigo asked on

Rewrite Rule to redirect .mysite.com to www.mysite.com

I have tried many different ways of achieving a rewrite that would 301 any request to .mysite.com to www.mysite.com and although I can accomplish that, it screws up other subdomains that I don't want to have be affected.

For instance. I have used code like:

RewriteCond %{HTTPS} (on)?
RewriteCond %{HTTP:Host} ^(?!www\.)(.+)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? http(?%1s)://www.%2%3 [R=301,L]

And that works, but it makes other subdomains that I need to have redirect correctly not work. An example would be:

cars.mysite.com would redirect with above rewrite rule to the following address:

www.cars.mysite.com 

That I of course don't want. Now the backstory is that I am permanently redirecting users from old subdomains because I no longer want to use subdomains except www. I am now using folders to seperate out sections of the site. I can successfully redirect people as long as I don't have the rewrite rule you see above, but it opens up problems because if I allow a wildcard in the DNS it allows anything to work and I don't want wwerwer.mysite.com to work or any other such bad urls.

Anybody encountered a similar problem of which they found a solution.
Microsoft IIS Web ServerWeb Development

Avatar of undefined
Last Comment
meverest

8/22/2022 - Mon
meverest

Hi,

how many domains are we talking about?  Is it feasible to write a rule for each one?

Cheers.

(ps: I assume that you know that the rewrite rule above is not quite right...?)
ASKER
tarrigo

We only have about 5 subdomains that we used to use. We just had a problem with a particular link somewhere that used iwww instead of www and google went and indexed a lot of pages under that subdomain. That worked because I had a wildcard in the DNS for any subdomain to be allowed.

The rule I use above isn't quite right as you said. At this point I just want all requests to http://mysite.com to 301 to www.mysite.com but most rules I have tried simply append the www to anything. So for instance, if I had a subdomain of http://cars.mysite.com the redirection would send the person to www.cars.mysite.com.

I am doing all this because I don't want there to be a split in google for http://mysite.com and http://www.mysite.com.

ASKER CERTIFIED SOLUTION
meverest

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck