-1

I'd like to rewrite a url to match the exact name of the file name of the page. I mean: I've a http://example.com/PagePage.aspx, and on typing on the browser bar http://example.com/pagepage.aspx I'd like to obtain again http://example.com/PagePage.aspx.

Of course, the same on typing any combination of characters, i.e. Pagepage.aspx, pagePage.aspx, and so on...

I tried in this way:

<system.webServer>
    <rewrite>
      <rules>
        <rule name="SpecificRewrite" stopProcessing="true">
          <match url="^pagepage$" />
          <match url="^Pagepage$" />
          <match url="^pagePage$" />
          <action type="Rewrite" url="PagePage.aspx" />
        </rule>
      </rules>
    </rewrite>
</system.webServer>

but I get "500 - Internal server error".

So I also tried in this way:

  <system.webServer>
    <rewrite>
      <rules>
        <rule name="SpecificRewrite" stopProcessing="true">
          <match url="^pagepage\/?$" />
          <match url="^Pagepage\/?$" />
          <match url="^pagePage\/?$" />
          <action type="Redirect" url="/PagePage.aspx" />
        </rule>
      </rules>
    </rewrite>
</system.webServer>

But I get an infinite loop

I tried to obtain the same using Global.asax

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpContext context = HttpContext.Current;
            string path = context.Request.Path;
            if (path.Equals("/pagepage.aspx"))
            {
                context.RewritePath(path.Replace("pagepage.aspx", "PagePage.aspx"));
// or context.RewritePath("PagePage.aspx"); is the same
            }
        }

But, again...I get an infinite loop erro...

I googled the issue but...the methods I found are, more or less, the same I've already tried. Where am I wrong?

Any suggestion about it? Thank you in advance

Nick
  • 25
  • 1
  • 2
  • 6

1 Answers1

0

First of all, you can't have more than one match element for a rule. That's probably why you get the 500 error. Second, I think you actually mean redirect action rather than rewrite. So the following, will check for requests ending pagepage.aspx regardless the letter case and the condition will check if url ends with PagePage.aspx so it doesn't go in infinite loop. Here it is:

<rule name="SpecificRewrite" stopProcessing="true">
 <match url="^pagepage.aspx$" />
 <conditions>
  <add input="{REQUEST_URI}" pattern="PagePage.aspx" ignoreCase="false" negate="true" />
 </conditions>
 <action type="Redirect" url="PagePage.aspx" />
</rule>
nanestev
  • 822
  • 8
  • 15