简体   繁体   中英

Rewrite rule not working on IIS with using Regex

I'm trying to write a rewrite rule in on Windows Server 6.2. Although I used IIS Manager to create the code, it didn't work.

I tried stopProcess true/false, used different regex, restart server several times. Nothing changed. I followed the whole steps on Microsoft's web site on https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module

<system.webServer>
    <rewrite>
      <rules>
        <rule name="Rewrite to e-campus" stopProcessing="true">
          <match url="[^\/]+\/\/([^\/]+:?[0-9]?)\/.*" />
          <action type="Rewrite" url="{R:1}" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>

I want to show only main URL. My domain is http://e-campus.example.com .

For example if someone go to that link: http://e-campus.example.com/Login/Student Server should rewrite to this: e-campus.example.com (with hiding http:// but it's not important)

So basically I just want to show main URL. But it keeps showing full path. What am I missing here?

According to your description, I found your regex match the whole url. But the iis url rewrite will not get the whole domain, it will just get the part of the url not the whole url.

For example:

If your url is http://e-campus.example.com/Login/Student ., the match url part is login/Student .

So if you want to rewrithe all the request to e-campus.example.com, you should use below url rewrite rule.

<system.webServer>
    <rewrite>
      <rules>
        <rule name="Rewrite to e-campus" stopProcessing="true">
          <match url="(.*)" />
          <action type="Rewrite" url="http://e-campus.example.com/" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM