<p>I'm using <a href="http://www.helicontech.com/isapi_rewrite/" rel="nofollow noreferrer">Helicon's ISAPI Rewrite 3</a>, which basically enables .htaccess in IIS.  I need to redirect a non-www URL to the www version, i.e. example.com should redirect to www.example.com.  I used the following rule from the examples but it affects subdomains:</p>

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

<p>This works for most part, but is also redirect sub.example.com to www.sub.example.com.  How can I rewrite the above rule so that subdomains do not get redirected?</p>

[{'answer_id': 50937, 'author': 'zigdon', 'author_id': 4913, 'author_profile': 'https://Stackoverflow.com/users/4913', 'pm_score': 0, 'selected': False, 'text': "<p>Can't you adjust the RewriteCond to only operate on example.com?</p>\n\n<pre><code>RewriteCond %{HTTP:Host} ^example\\.com(.*) [NC]\n</code></pre>\n"}, {'answer_id': 50948, 'author': 'Vinko Vrsalovic', 'author_id': 5190, 'author_profile': 'https://Stackoverflow.com/users/5190', 'pm_score': 3, 'selected': True, 'text': "<p>Append the following RewriteCond:</p>\n\n<pre><code>RewriteCond %{HTTP:Host} ^[^.]+\\.[a-z]{2,5}$ [NC]\n</code></pre>\n\n<p>That way it'll only apply the rule to nondottedsomething.uptofiveletters as you can see, subdomain.domain.com will not match the condition and thus will not be rewritten.</p>\n\n<p>You can change [a-z]{2,5} for a stricter tld matching regex, as well as placing all the constraints for allowed chars in domain names (as [^.]+ is more permissive than strictly necessary).</p>\n\n<p>All in all I think in this case that wouldn't be necessary.</p>\n\n<p>EDIT: sadie spotted a flaw on the regex, changed the first part of it from [^.] to [^.]+</p>\n"}, {'answer_id': 51039, 'author': 'dlamblin', 'author_id': 459, 'author_profile': 'https://Stackoverflow.com/users/459', 'pm_score': 1, 'selected': False, 'text': '<p>I\'ve gotten more control using <a href="http://urlrewriter.net/" rel="nofollow noreferrer">urlrewriter.net</a>, something like:</p>\n\n<pre><code>&lt;unless header="Host" match="^www\\."&gt;\n   &lt;if url="^(https?://)[^/]*(.*)$"&gt;\n     &lt;redirect to="$1www.domain.tld$2"/&gt;\n   &lt;/if&gt;\n   &lt;redirect url="^(.*)$" to="http://www.domain.tld$1"/&gt;\n&lt;/unless&gt;\n</code></pre>\n'}, {'answer_id': 51075, 'author': 'Josh Hunt', 'author_id': 2592, 'author_profile': 'https://Stackoverflow.com/users/2592', 'pm_score': 0, 'selected': False, 'text': '<p>Why dont you just have something like this in your vhost (of httpd) file?</p>\n\n<pre><code>ServerName: www.example.com\nServerAlias: example.com\n</code></pre>\n\n<p>Of course that wont re-direct, that will just carry on as normal</p>\n'}, {'answer_id': 51079, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': "<p>Zigdon has the right idea except his regex isn't quite right.  Use</p>\n\n<p><code>^example\\.com$</code></p>\n\n<p>instead of his suggestion of:</p>\n\n<p><code>^example\\.com(.*)</code></p>\n\n<p>Otherwise you won't just be matching example.com, you'll be matching things like example.comcast.net, example.com.au, etc.</p>\n"}, {'answer_id': 52578, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>@Vinko</p>\n\n<p>For your generic approach, I\'m not sure why you chose to limit the length of the TLD in your regex?  It\'s not very future-proof, and I\'m unsure what benefit it\'s providing?  It\'s actually not even "now-proof" because there\'s at least one 6-character TLD out there (.museum) which won\'t be matched.</p>\n\n<p>It seems unnecessary to me to do this.  Couldn\'t you just do <code>^[^.]+\\.[^.]\\+$</code>? (note: the question-mark is part of the sentence, not the regex!)</p>\n\n<p>All that aside, there is a bigger problem with this approach that is: it will fail for domains that aren\'t directly beneath the TLD.  This is domains in Australia, UK, Japan, and many other countries, who have hierarchies: .co.jp, .co.uk, .com.au, and so on.</p>\n\n<p>Whether or not that is of any concern to the OP, I don\'t know but it\'s something to be aware of if you\'re after a "fix all" answer.</p>\n\n<p>The OP hasn\'t yet made it clear whether he wants a generic solution or a solution for a single (or small group) of known domains.  If it\'s the latter, see my other note about using Zigdon\'s approach.  If it\'s the former, then proceed with Vinko\'s approach taking into account the information in this post.</p>\n\n<p>Edit: One thing I\'ve left out until now, which may or may not be an option for you business-wise, is to go the other way.  All our sites redirect <a href="http://www.domain.com" rel="nofollow noreferrer">http://www.domain.com</a> to <a href="http://domain.com" rel="nofollow noreferrer">http://domain.com</a>.  The folks at <a href="http://no-www.org" rel="nofollow noreferrer">http://no-www.org</a> make a pretty good case (IMHO) for this being the "right" way to do it, but it\'s still certainly just a matter of preference.  One thing is for sure though, it\'s far easier to write a generic rule for that kind of redirection than this one.</p>\n'}, {'answer_id': 52919, 'author': 'Vinko Vrsalovic', 'author_id': 5190, 'author_profile': 'https://Stackoverflow.com/users/5190', 'pm_score': 1, 'selected': False, 'text': "<p>@org 0100h Yes, there are many variables left out of the description of the problem, and all your points are valid ones and should be addressed in the event of an actual implementation. There are both pros and cons to your proposed regex. On the one hand it's easier and future proof, on the other, do you really want to match example.foobar if sent in the Host header? There might be some edge cases when you'll end up redirecting to the wrong domain. A thrid alternative is modifying the regex to use a list of the actual domains, if more than one, like </p>\n\n<pre><code>RewriteCond %{HTTP:Host} (example.com|example.net|example.org) [NC]\n</code></pre>\n\n<p>(Note to chris, that one <strong>will</strong> change %1)</p>\n\n<p>@chrisofspades It's not meant to replace it, your condition number two ensures that it doesn't have www, whereas mine doesn't. It won't change the values of %1, %2, %3 because it doesn't store the matches (iow, it doesn't use parentheses).</p>\n"}]
['https://Stackoverflow.com/questions/50931', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2614/']