Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
912 views
in Technique[技术] by (71.8m points)

iis - Remove multiple forward slashes

I've noticed that with .NET MVC sites, you're able to hit URLs with multiple forward slashes, for example:

http://www.example.com//category
http://www.example.com//category//product

The URL loads fine and everything works, however, I've been asked to stop this from happening.

I've been trying to use IIS URL Rewrites to get it working:

<rewrite>
    <rules>
        <rule name="Remove multiple slashes" stopProcessing="true">
            <match url="(.*)" />
            <conditions>
                <add input="{UNENCODED_URL}" matchType="Pattern" pattern="^(.*)//(.*)$" />
            </conditions>
            <action type="Redirect" redirectType="Permanent" url="{C:1}/{C:2}" />
         </rule>
    </rules>
</rewrite>

However, the results seem very temperamental. Sometimes the product URL will redirect, sometimes it won't and the same thing happens with the category. It's almost like the URL is being cached by the application.

Does anyone know if I can disable any caching that's in place, or if there is another way to get around this multiple slash issue?

Any help is much appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In the end, I have resorted to using a code behind redirect to get it working.

The issues I was having using the IIS URL Rewrites was due to the way IIS caches the redirects. When I disabled caching completely, as WouterH suggested, it worked. However, I'm not comfortable disabling caching in this way as it could introduce performance issues.

My fix was to use a code behind redirect in the Global.asax.cs file:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    string requestUrl = Request.ServerVariables["REQUEST_URI"];
    string rewriteUrl = Request.ServerVariables["UNENCODED_URL"];
    if (rewriteUrl.Contains("//") && !requestUrl.Contains("//"))
        Response.RedirectPermanent(requestUrl);
}

I would have liked to use IIS URL Rewrite to get this working, unfortunately I didn't have the time to continue down that line.

Interestingly, the below method did work, however, the HTTP_X_REWRITE_URL is added by the Helicon ISAPI Rewrite which I'm running locally, but is not available on our production server.

<rewrite>
  <rules>
    <rule name="Remove multiple slashes" stopProcessing="true">
      <match url=".*" />
      <action type="Redirect" url="{REQUEST_URI}" />
      <conditions>
        <add input="{HTTP_X_REWRITE_URL}" pattern="([^/]*)/{2,}([^/]*)" />
      </conditions>
    </rule>
  </rules>
</rewrite>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...