Redirecting a non-www URL to www using .htaccess

I use Helicon ISAPI Rewrite 3 , which basically allows .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 examples, but it affects subdomains:

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

This works for the most part, but also redirects sub.example.com to www.sub.example.com. How can I rewrite the above rule so that subdomains are not redirected?

+6
.htaccess isapi-rewrite
source share
7 answers

Add the following RewriteCond:

 RewriteCond %{HTTP:Host} ^[^.]+\.[az]{2,5}$ [NC] 

Thus, it will only apply the rule to an invalid number of images, as you can see, subdomain.domain.com will not meet the condition and therefore will not be overwritten.

You can change [az] {2,5} for the stricter tld regular expression, as well as put all restrictions on allowed characters in domain names (since [^.] + Is more permissive than is strictly necessary).

In general, I think that in this case it would not be necessary.

EDIT: sadie noticed a lack of regular expression, changed the first part of it from [^.] To [^.] +

+3
source share

I got more control using urlrewriter.net , something like:

 <unless header="Host" match="^www\."> <if url="^(https?://)[^/]*(.*)$"> <redirect to="$1www.domain.tld$2"/> </if> <redirect url="^(.*)$" to="http://www.domain.tld$1"/> </unless> 
+1
source share

Zigdon has the right idea, except that his regular expression is not entirely correct. Use

^example\.com$

instead of his suggestion:

^example\.com(.*)

Otherwise, you will not just match example.com, you will match things like example.comcast.net, example.com.au, etc.

+1
source share

@Vinko

For your general approach, I'm not sure why you decided to limit the length of the TLD in your regular expression? This is not a very secure future, and I'm not sure what benefit it provides? This is actually not even “now proof”, because there is at least one 6-digit TLD (.museum) that will not match.

It seems unnecessary to do this. Could you just do ^[^.]+\.[^.]\+$ ? (note: the question mark is part of the sentence, not a regular expression!)

All aside, there is a big problem with this approach: it will fail for domains that are not directly under the TLD. These are domains in Australia, the UK, Japan, and many other countries that have hierarchies: .co.jp, .co.uk, .com.au, etc.

Regardless of whether this applies to the OP, I do not know, but this is what you need to know if you answer “fix everything” after the answer.

The OP has not yet made it clear whether he wants a universal solution or a solution for one (or a small group) of known domains. If this is the latter, see My other note on using the Zigdon approach. If this is the first, then proceed to the Vinko approach, given the information in this post.

Edit: One thing that I still have left so far, which may or may not be a business option for you, is to go the other way. All our sites redirect http://www.domain.com to http://domain.com . People at http://no-www.org make a pretty good case (IMHO) because this is the "right" way to do it, but it's still just a matter of preference. One thing is certain, but it’s much easier to write a general rule for such a redirect than that.

+1
source share

@org 0100h Yes, there are many variables left from the description of the problem, and all your points are valid and should be considered in case of real implementation. There are pros and cons to your proposed regular expression. On the one hand, it is simpler and a proof in the future, on the other hand, do you really want to match example.foobar if it is sent in the Host header? There may be some edge issues when you go to the wrong domain. A quiet alternative is to change the regular expression to use a list of actual domains, if more than one, for example

 RewriteCond %{HTTP:Host} (example.com|example.net|example.org) [NC] 

(Note to chris that one will be changed% 1)

@chrisofspades It is not intended to be a substitute, your condition number two ensures that it does not have www, whereas I do not. It will not change the values ​​of% 1,% 2,% 3, because it does not preserve matches (that is, it does not use parentheses).

+1
source share

Can you customize RewriteCond only on example.com?

 RewriteCond %{HTTP:Host} ^example\.com(.*) [NC] 
0
source share

Why don't you have anything like this in your vhost (httpd) file?

 ServerName: www.example.com ServerAlias: example.com 

Of course, this will not be redirected, it will continue as usual.

0
source share

All Articles