Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Possible Duplicate:
Everything You Ever Wanted to Know about Mod_Rewrite Rules but Were Afraid to Ask?

The below code adds www. to any url that does not start with it:

RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^ https://www.%2%{REQUEST_URI} [L,R=301]

However, I want it to do this only when the url is of the format:

If the url is like:

I do not want the rule adding www.

How do I change this?

share|improve this question

marked as duplicate by Chris S Feb 17 '12 at 16:23

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


RewriteCond %{HTTP_HOST} !^www\.


RewriteCond %{HTTP_HOST} !(^www\.|\..+\.)

In other words, change your conditional from looking for "www" to looking for www or looking for two dots.

Though, given the problem description, this would probably work, too, and has the advantage of being simpler:

RewriteCond %{HTTP_HOST} !\..*\.

The problem could probably be described better as "rewrite any host with only one dot to prepend "www.".

share|improve this answer
Unfortunately both those changes still try to add www. to any url – psynnott Apr 1 '10 at 8:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.