4

I really like the IIS7 URL rewriting module and so far, it worked great for me.

There is one thing that I'm not sure how to do: I would like to permanently redirect all URLs that have encoded spaces (%20) in them to a URL that has the spaces replaced with a dash (-).

So this:

http://www.test.com/About%20Our%20Mission.aspx

should be redirected to this:

http://www.test.com/About-Our-Mission.aspx

Is that even possible with only regular expressions?

Stefan
  • 1,719
  • 2
  • 15
  • 27

5 Answers5

6

There's no way to do directly what you want.

You might settle for something like this:

^(.*)%20(.*)%20(.*)%20(.*)  replaced by:  {R:1}-{R:2}-{R:3}-{R:4}
^(.*)%20(.*)%20(.*)         replaced by:  {R:1}-{R:2}-{R:3}
^(.*)%20(.*)                replaced by:  {R:1}-{R:2}
Jeremy Stein
  • 19,171
  • 16
  • 68
  • 83
  • That's pretty much exactly what I have right now(note that %20 is interpreted as a whitespace by IIS Rewrite): Pattern: (\w+)\s+(\w+)\s+(\w+)\s+(\w+.aspx) Redirect URL: {R:1}-{R:2}-{R:3}-{R:4} Works pretty well, but I also have paths like About%20Us/Blah%20Dah.aspx. I would need a whole bunch of rules to deal with all possible spaces. – Stefan Jun 29 '09 at 17:34
  • 2
    You won't need as many rules if you count down by powers of 2, instead of by 1, and allow processing of subsequent rules. For example, rules like those above to match 16 spaces, then 8 spaces, then 4, then 2, then 1. When used in that order, they will match any number of spaces up to 31, and will do so with 5 rules instead of 31 rules – jproch Sep 29 '17 at 15:23
  • @jproch great idea. I think @JeremyStein's answer in general is a great solution to a frustrating problem. If you're realistic about the length of your urls and how many `%20`s you're likely to ever have, you can cover all bases with a handful of easy-to-implement rules. Then you don't have to go down the route of writing a custom request handler/ url rewriter which is *yet another* thing to maintain. – theyetiman Dec 12 '17 at 15:02
2

One of then nice things about .aspx is how easy it is to rewrite URLs with real code. Just add a little search and replace code to your web site's Global.asax file:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    string path = HttpContext.Current.Request.Path;
    // Search and replace, RegEx, etc.
    HttpContext.Current.RewritePath(path);
}

On IIS7, you have to add some entries in web.config to handle rewriting non .aspx URLs:

<system.webServer>
    <handlers>
        <clear/>
        <add name="Brands1" path="Brands/*.html" verb="*" type="ASP.global_asax" resourceType="Unspecified"/>
        <add name="Brands2" path="Brands/\?*.html" verb="*" type="ASP.global_asax" resourceType="Unspecified"/>
        <!-- ... -->

The IIS7 URL rewriting module is great, but just because you have a hammer...

P.J. Tezza
  • 131
  • 1
  • 4
1

The same may be achieved in one rule with ISAPI_Rewrite 3 or Helicon Ape for any number of %20s:

RewriteBase /
RewriteRule ^(.*)%20(.*)$ $1-$2 [LP,R=301,L]
TonyCool
  • 1,004
  • 1
  • 6
  • 5
1

You can write Custom Rewrite Provider to do any manipulation you want with the original url. But that involves more than regular expression only. More details here.

Tomek
  • 3,267
  • 2
  • 22
  • 23
0

Perhaps I'm mad, but this seems to work...

Use a URL_Rewrite rule using Regular Expressions with this pattern:

^(.*) (.*)

Redirect to

{R:1}-{R:2}

I've tested this with a single space or many spaces and it works fine for me using IIS 10. Note that it works just as well for %20 as it does for "" in the URL string, cheers.

Chrᴉz remembers Monica
  • 1,829
  • 1
  • 10
  • 24
Nathan
  • 1