Regex URL match on anything but www

时光毁灭记忆、已成空白 提交于 2020-01-16 13:17:30

问题


I'm using IIS7 and the URL Rewrite module.

I would like to use regex to match any subdomain apart from www.

So...

frog.domain.co.uk = Match
m.domain.co.uk = Match
anything.domain.co.uk = Match
www.domain.co.uk = No match

This way I can redirect any subdomain that someone types in back to www.


回答1:


you can use 301 in .htaccess for this.




回答2:


This will match what you want:

^(?!=www\.).*

Which is a negative lookahead for www.. Not sure if you need the trailing .*




回答3:


Use this rule -- it will redirect to www.exmaple.com domain if domain is different:

<system.webServer>
      <rewrite>
          <rules>
              <rule name="Force www" stopProcessing="true">
                  <match url="(.*)$" />
                  <conditions>
                      <add input="{HTTP_HOST}" pattern="^www\.example\.com" negate="true" />
                  </conditions>
                  <action type="Redirect" url="http://www.example.com/{R:1}" />
              </rule>
          </rules>
      </rewrite>
  </system.webServer>

You can optimize it a bit if you do not want to type domain name twice (example.com) -- but that is very minor thing and depending on your circumstances/configuration it is can be undesired.



来源:https://stackoverflow.com/questions/6706155/regex-url-match-on-anything-but-www

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!