1

RE: .htaccess - how to force "www." in a generic way?

I asked this question before, and got this answer:

RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

It works, but now I am seeing 301 Moved Permanently in the response headers. I want to eliminate the 301s. Is the problem the 2nd rewrite condition? Should it be something like "does not start with 'www.' followed by the host name"?

By the way, I want this solution to work for any server (meaning, I don't want to hard code my domain name).

Suggestions?

UPDATE:

I just realized that the above is not working correctly. If I have the following:

http://images.domain.com

I don't want that to change to:

http://www.images.domain.com

I don't want this affecting sub-domains. I only want it to affect missing www.

Community
  • 1
  • 1
StackOverflowNewbie
  • 35,023
  • 98
  • 252
  • 421
  • Re making the solution work on any server, it should do that already, shouldn't it? – Pekka Mar 11 '11 at 11:20
  • @Pekka, this one does. I just wanted to tell the experts here so that they don't provide me with an answer that involves hard coding my domain name. – StackOverflowNewbie Mar 11 '11 at 11:30
  • @Stack well, I think what you want to do can't be done. Why is the 301 a problem? – Pekka Mar 11 '11 at 11:41
  • How do you plan to perform the redirect without telling the browser about it? – Dave Child Mar 11 '11 at 12:08
  • @Pekka - two of my 301 redirects cost about 260ms (images). The other one cost about 520ms (CSS). I'm trying to see what I can do to optimize the speed of the site. – StackOverflowNewbie Mar 11 '11 at 12:56
  • @Stack 260ms for a redirect? That sounds odd. But why do they happen in the first place - why do you have image and CSS links pointing to the undesired server address? – Pekka Mar 11 '11 at 13:00
  • @Pekka, say the image is at http://images.domain.com/logo.gif. What's happening is that it's changing to http://www.images.domain.com/logo.gif. There's a bug in the rewrite condition, I think. – StackOverflowNewbie Mar 11 '11 at 19:50

1 Answers1

2

Redirecting to a different domain is by definition not possible without some header redirect. If you want the URL in the user's browser to change, you have to force a new request. There is no way around that.

You will have to take your pick - the 301, 302 and 303 status codes being the most straight forward choices.

Pekka
  • 418,526
  • 129
  • 929
  • 1,058
  • I've updated my question with an additional problem. Are you able to help? I'm a bit lost now. – StackOverflowNewbie Mar 11 '11 at 13:00
  • @StackOverflow an additional `RewriteCond %{HTTP_HOST} ^example.com$` would fix it but that would bind it to specific domains. As to how to exclude `images.*`, maybe ask Gumbo in the other question, he knows more about this stuff – Pekka Mar 11 '11 at 13:02
  • do you know how I can make the rule say "does not begin with a www followed by a dot followed by the HTTP HOST"? That's the only time I think I want to rewrite. – StackOverflowNewbie Mar 11 '11 at 19:54
  • @Stack mmm, if I'm not mistaken, if I have `www.` in front, then HTTP_HOST consists of www. plus the main domain name, doesn't it? – Pekka Mar 11 '11 at 20:19
  • ohh! You are right! This has become a bigger headache, I think. – StackOverflowNewbie Mar 11 '11 at 23:16