February 14, 2020 10:07 by
Peter
At this article, I’m going to tell you how search out and replace any “_” (underscore) characters in a URL .htm file name and replace it with “-” (dash). The list I used to be given had file names with up to 7 underscores in any position in IIS 7.5. Example: my_file_name.htm
While I figured this might be a straight-forward task with IIS URL Rewrite, I used to be wrong. At the end I found that I either had to form one rule for every possible underscore count or write a custom rewrite rule. I went the one rule per count route. I scan in one journal you'll only spend to nine variables (). The other a part of the rule was they'd to be only in the “/articles/” directory.
My first challenge was simply to get regular expression in place. What I seen was that the IIS 7.5 UI’s “Test Pattern” utility doesn’t accurately test. Within the test this worked:
Input: http://www.webtest.com/articles/myfilename.htm
Pattern: ^.*\/articles\/(.*)_(.*).htm$
Capture Groups: {R:1} : "my", {R:2} : "test"
However, this doesn’t match in real-world testing. #1, don’t escape “/” (forward-slash). #2 the pattern is only matched against everything after the domain and first slash. So really, only this works:
Input: http://www.test.com/articles/myfilename.htm
Pattern: ^articles/(.*)_(.*).htm$
Capture Groups: {R:1} : "my", {R:2} : "test"
When order to match against up to 8 underscores, you need 8 rules, each one looking for more underscores. So, Here is the code that I used:
Input: http://www.test.com/articles/myfilename.htm
Pattern: ^articles/(.*)_(.*)_(.*).htm$
Capture Groups: {R:1} : "my", {R:2} : "test", {R:3} : "file"
To do this with efficiency you only edit the web.config in the web root for that website. the end result concluded up being:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="AUSx1" stopProcessing="true">
<match url="^articles/(.*)_(.*).htm$" />
<action type="Redirect" url="articles/{R:1}-{R:2}.htm" />
</rule>
<rule name="AUSx2" stopProcessing="true">
<match url="^articles/(.*)_(.*)_(.*).htm$" />
<action type="Redirect" url="articles/{R:1}-{R:2}-{R:3}.htm" />
</rule>
<rule name="AUSx3" stopProcessing="true">
<match url="^articles/(.*)_(.*)_(.*)_(.*).htm$" />
<action type="Redirect" url="articles/{R:1}-{R:2}-{R:3}-{R:4}.htm" />
</rule>
<rule name="AUSx4" stopProcessing="true">
<match url="^articles/(.*)_(.*)_(.*)_(.*)_(.*).htm$" />
<action type="Redirect" url="articles/{R:1}-{R:2}-{R:3}-{R:4}-{R:5}.htm" />
</rule>
<rule name="AUSx5" stopProcessing="true">
<match url="^articles/(.*)_(.*)_(.*)_(.*)_(.*)_(.*).htm$" />
<action type="Redirect" url="articles/{R:1}-{R:2}-{R:3}-{R:4}-{R:5}-{R:6}.htm" />
</rule>
<rule name="AUSx6" stopProcessing="true">
<match url="^articles/(.*)_(.*)_(.*)_(.*)_(.*)_(.*)_(.*).htm$" />
<action type="Redirect" url="articles/{R:1}-{R:2}-{R:3}-{R:4}-{R:5}-{R:6}-{R:7}.htm" />
</rule>
<rule name="AUSx7" stopProcessing="true">
<match url="^articles/(.*)_(.*)_(.*)_(.*)_(.*)_(.*)_(.*)_(.*).htm$" />
<action type="Redirect" url="articles/{R:1}-{R:2}-{R:3}-{R:4}-{R:5}-{R:6}-{R:7}-{R:8}.htm" />
</rule>
<rule name="AUSx8" stopProcessing="true">
<match url="^articles/(.*)_(.*)_(.*)_(.*)_(.*)_(.*)_(.*)_(.*)_(.*).htm$" />
<action type="Redirect" url="articles/{R:1}-{R:2}-{R:3}-{R:4}-{R:5}-{R:6}-{R:7}-{R:8}-{R:9}.htm" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
In the end this URL:
http://www.yourdomain.com/articles/my_file_foo_bar.htm
Becomes:
http://www.yourdomain.com/articles/my-file-foo-bar.htm