Redirecting non-www URL to www using .htaccess

后端 未结 7 1550
悲&欢浪女
悲&欢浪女 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条回答
  •  梦毁少年i
    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 [^.]+

提交回复
热议问题