Redirecting non-www URL to www using .htaccess

后端 未结 7 1510
悲&欢浪女
悲&欢浪女 2021-01-05 12:04

I\'m using Helicon\'s ISAPI Rewrite 3, 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 ww

相关标签:
7条回答
  • 2021-01-05 12:11

    Why dont you just have something like this in your vhost (of httpd) file?

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

    Of course that wont re-direct, that will just carry on as normal

    0 讨论(0)
  • 2021-01-05 12:12

    Zigdon has the right idea except his regex isn't quite right. Use

    ^example\.com$

    instead of his suggestion of:

    ^example\.com(.*)

    Otherwise you won't just be matching example.com, you'll be matching things like example.comcast.net, example.com.au, etc.

    0 讨论(0)
  • 2021-01-05 12:21

    Append the following RewriteCond:

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

    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.

    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).

    All in all I think in this case that wouldn't be necessary.

    EDIT: sadie spotted a flaw on the regex, changed the first part of it from [^.] to [^.]+

    0 讨论(0)
  • 2021-01-05 12:21

    @Vinko

    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.

    It seems unnecessary to me to do this. Couldn't you just do ^[^.]+\.[^.]\+$? (note: the question-mark is part of the sentence, not the regex!)

    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.

    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.

    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.

    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 http://www.domain.com to http://domain.com. The folks at http://no-www.org 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.

    0 讨论(0)
  • 2021-01-05 12:24

    I've gotten 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>
    
    0 讨论(0)
  • 2021-01-05 12:30

    @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

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

    (Note to chris, that one will change %1)

    @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).

    0 讨论(0)
提交回复
热议问题