Brady Moritz
Brady Moritz

Reputation: 8903

iis rewrite url with dot at end of url

I have some external links coming into my site which mistakenly had a period added to their ends. (so, I can't fix them). Since inbound links are always good, I want to redirect these links to a legit page. I've tried a number of rules in the urlrewrite module for iis.5 and nothing seems to capture this url.

I've seen other questions on here regarding asp.net urls with periods at the end, but I'm trying to capture this one at the IIS rewrite module level. Any pointers on making this work?

Upvotes: 3

Views: 3116

Answers (4)

Yossi G.
Yossi G.

Reputation: 1161

On IIS 8.5 I implemented something substantially similar to that proposed by GWR but with one subtle change. I needed the attribute appendQueryString="true" on the Rewrite action tag. (Perhaps this was the default back in 2016, but now needs to be specified.)

Here's what worked for me:

  <system.webServer>
    <modules>
    ...
    </modules>
    <validation validateIntegratedModeConfiguration="false" />
    <rewrite>
        <rules>
            <rule name="RemoveTrailingDots">
                <match url="^(.*[^.])\.+$" />
                <action type="Rewrite" url="{R:1}" appendQueryString="true" />
            </rule>
        </rules>
    </rewrite>
  </system.webServer>

I had to install the URL Rewrite feature from here: https://www.iis.net/downloads/microsoft/url-rewrite

Upvotes: 0

GWR
GWR

Reputation: 2008

I used a solution that removes all trailing dots at the web.config level:

<rule name="RemoveTrailingDots" stopProcessing="false">
    <match url="^(.*[^.])\.+$" />
    <action type="Rewrite" url="{R:1}" />
</rule>

This rule just rewrites the request url internally at the beginning of my rewrite rules, allowing the request to process through the normal rules instead of being caught upstream

If you don't have any more rules to apply, and just want to redirects anything with dots at the end, just change the action type to Redirect (case sensitive)

Upvotes: 1

ITmeze
ITmeze

Reputation: 1922

This is quite an old one but as i was facing similar issue recently i've decided to post my findings...

I assume that one of modules that run prior to url-rewrite module terminates, with an exception, preventing request from continuing to url-rewrite (i suspect some security checks). Thus it is not possible to resolve your issue with url-rewrite.

One possible workaround could be to redirect requests ending with dot '.' at the very early stage in Global.asax, like here:

protected void Application_PreSendRequestHeaders(object sender, EventArgs e)
{
    if (Context.Request.RawUrl.EndsWith(".aspx."))
    {
        //permanent redirect and end current request...
        Context.Response.RedirectPermanent(Context.Request.RawUrl.TrimEnd('.'), true);
    }
}

That might be far from optimal answer but at least it gets job done ;)

Upvotes: 2

Michael Cox
Michael Cox

Reputation: 1301

If you have a fixed set of inbound links that are incorrect, you can concentrate on redirecting the main part of the URL, rather than the dot.

For example, with this URL: http://www.example.com/index.html.

Have your IIS Rewrite look for a URL matching "^index.html.*" and redirect to the page you need. That should catch the URL both with and without the dot.

Upvotes: 0

Related Questions