Redirecting non-www URL to www using .htaccess

后端 未结 7 1512
悲&欢浪女
悲&欢浪女 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: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).

提交回复
热议问题