IIS Rewrite Rule using Url Path segments - iis

I'm trying to write, hopefully, a single rewrite rule for a number of URLs that are being moved to a different structure. All of the URLs have a very similar structure so I'm thinking there must be a way to write a single rule for all of them.
Example:
http://example.com/some/deep/path/a/001/my-a-data
http://example.com/some/deep/path/a/002/my-a-data2
http://example.com/some/deep/path/b/001/my-b-data
http://example.com/some/deep/path/b/002/my-b-data2
http://example.com/some/deep/path/c/001/my-c-data
http://example.com/some/deep/path/c/002/my-c-data2
Need to redirect to (respectively):
http://example.com/a/my-a-data
http://example.com/a/my-a-data2
http://example.com/b/my-b-data
http://example.com/b/my-b-data2
http://example.com/c/my-c-data
http://example.com/c/my-c-data2
So what I'm trying to know is if there is a way to read the segments of the PATH in the URL so I can just reuse them to build the final URL because it is all there. It is just a simplification of the URL.
Perhaps to do something like
/{1}/{2}/{3}/{4}/{5}/{6}
http://example.com/some/deep/path/a/001/my-a-data -> http://example.com/{4}/{6}
Is that possible?
Thanks!!!

I found how it works.
Each segment in the URL path can be identified with {R} parameters in the rewrite rules. To match my URL structure I created the following rule:
<rule name="Documents redirection" stopProcessing="true">
<match url="^some/deep/path/(.*)/(.*)/(.*)$" ignoreCase="true" />
<action type="Redirect" url="/{R:1}/{R:3}" redirectType="Permanent" />
</rule>
Each of the (.*) segments are what the IIS Rewrite module recognizes as {R} parameter, so for my need, I needed the first and third parameter to form a new URL. I can safely ignore the second parameter.
I hope this helps someone else.

It is recommended to use([^/])+ instead of (.*) .
If there are multiple slash in your URL, the capture group would be corrupted.
For example:
Best regards,
Sam

Related

IIS URL rewrite has a problem with non-English characters

I set the IIS URL rewrite in my web.config of my asp.net project.
It's working well with the English alphabet in the title query string, but it doesn't work with a none English alphabet, even when I changed the title to {UrlEncode:{R:2}}.
<rule name="Rewrite to Page.aspx">
<match url="^page/([0-9]+)/([_0-9a-z-]+)"/>
<action type="Rewrite" url="page.aspx?id={R:1}&title={R:2}"/>
</rule>
for example:
http://localhost:53849/page/1/english-character is working properly, but http://localhost:53849/page/1/ตรวจสอบ is not working and throws the following error:
HTTP Error 404.0 - Not Found
You can also change it to
^page/([0-9]+)/(.*)
By the way, if the question has been solved, please post solution as answer and mark it so that it can help others.

Space getting added in IIS' URL rewrite rule when a new line exists in web.config

I have provided a PDF document containing sample URL rewrite rules to setup reverse proxy to our customers.
When they copy the config to their web.config, if the match URL overflows to the next line (a space gets added.
In the below example, when they copy the below configuration,
<rule name="ReverseProxyInboundRule1" enabled="true" stopProcessing="true">
<match url="(xxyyzzURL|TestService|aabbccddeeffURL|
testAbcdefURL/)(.*)" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false" />
<action type="Rewrite" url="https://example.com/{R:0}" />
</rule>
the URL rewrite will have a space before testAbcdefURL and doesn't match this URL
xxyyzzURL|TestService|aabbccddeeffURL| testAbcdefURL
Is there a way to fix the issue withour giving instruction to customer to remove the whitespaces?
Through my test, it may be difficult to achieve because it is contrary to the setting of IIS to read the web.config file.
Mostly,few people add a new line in config file, especially the content in an attribute of a tag. So I have two ideas, you can refer to and make a decision according to your situation.
Use regex to match URL. I can’t exactly know the URL you want to match, so you can write it by yourself or use a regex generator online. To a certain extent, regex can shorten the match pattern unless the original URL is not long.
Edit the font and size on the pdf file so that the match url will not display a new line when the file is sent to your customers.
Suggest your customers use IIS GUI. It is easy to learn and use by refer to Microsoft document.

IIS Rewrite: why does IIS show the rewrite URL?

I use IIS Rewrite Module. I have a few static files, and they are stored at
website/static/xyz/abc
I added IIS Rewrite rule as follows:
regular expression pattern:
xyz/.+
rewrite URL:
/static/{R:0}
First, if I type (with an ending /)
http://mysite/xyz/abc/
everything seems working just fine. The browser address bar shows exactly what I typed.
Second, If I type (without the ending /)
http://mysite/xyz/abc
the website still displays the content correctly, but the browser address bar changes to (by adding static and the ending /)
http://mysite/static/xyz/abc/
How can I make IIS not show /static in the second situation?
Thanks and regards.
UPDATE
Regular expression is used.
<rule name="inauguralball">
<match url="inauguralball(/.*)?" />
<action type="Rewrite" url="/static/{R:0}" />
</rule>
I think I found your answer here: http://support.microsoft.com/kb/298408/EN-US. When you leave out the trailing slash and IIS finds a directory that matches your request, it performs a redirect for you. I believe this is where your redirect is coming from, thereby sending you to /static/inauguralball/. Notice that IIS has also added the trailing slash for you because it detected that directory exists.
Can you make sure that your hrefs always point to /inauguralball/ (with the trailing slash) so that IIS doesn't perform that automatic redirect for you?

URL Redirect with BlogEngine

I am in the process of moving Blog from dasBlog to BlogEngine. In dasBlog, my URLs looks like this.
http://pfsolutions-mi.com/blog/2008/03/08/Beyond-Compare.aspx
Whereas in BlogEngine, my URLs looks like this.
http://pfsolutions-mi.com/blog/post/2008/03/08/Beyond-Compare.aspx
The only difference between the two URLs is the "post" sub-folder in BlogEngine.
Since I'm currently using IIS URL Rewrite to remove WWW from the URL, I figured the easiest solution would be to create another rule to handle adding the sub-folder. I tried something like this.
rule name="Blog Redirect" enabled="true" stopProcessing="true"
match url="^blog/([_0-9]+)/([_0-9]+)/([_0-9]+)/([_0-9a-z-]+).([_0-9a-z-]+)$"
action type="Redirect" url="blog/post/{R:1}/{R:2}/{R:3}/{R:4}.{R:5}" redirectType="Temporary"
However, when I enter an old dasBlog URL it does not get redirected to the new location. Instead I get the generic BlogEngine 404 Error page.
Note: I plan to change the redirectType to Permanent once I know everything is working.
Shouldn't your matching regex look more like this?
match url="^blog/([0-9]+)/([0-9]+)/([0-9]+)/([\w-]+)\.([a-z]+)$"
There are no underlines in date numbers anyway and your [_0-9a-z-]+ doesn't include uppercase letters like in "Beyond-Compare".
So here we should have: url="^blog/digits/digits/digits/any-word-characters.lowercase-letters$"
We could also specify more with:
match url="^blog/([0-9]{2,4})/([0-9]{2})/([0-9]{2})/([\w-]+)\.([a-z]{3,4})$"
Based on the assumption that you always have:
Year as "08" or "2008"
Month and day as "01" or "11"
File endings with 3 or 4 lowercase letters (htm, html, php, asp, aspx, etc.)
EDIT: I think "\w+" does not include hyphen, so you must turn this into "[\w-]+"

IIS URL Rewriting

Using the URL Rewrite module, I've got a rule setup that is defined as:
Matches the pattern
Regular Expressions
Pattern: /support/viewmessages.aspx
Ignore Case: true
Action Type: Redirect
Redirect URL: http://newdomain/support/viewmessages.aspx
Append Query String: true
Redirect Type: 301
This sounds to me like it should redirect any of the URLs formated like:
http://olddomain/support/viewmessages.aspx?forum=20&topic=75942&ForumName=General%20Discussion
To be:
http://newdomain/support/viewmessages.aspx?forum=20&topic=75942&ForumName=General%20Discussion
However, nothing seems to be happening. I'm getting my 404 on the old domain, and it's not going to the 404 with the query string appended.
Here's the code that IIS generated in my web.config:
<rule name="Forum Posts" patternSyntax="ECMAScript" stopProcessing="true">
<match url="/support/viewmessages.aspx" />
<action type="Redirect" url="http://newdomain/support/viewmessages.aspx" appendQueryString="true" />
</rule>
Any help would be appreciated.
Attempt 1: Did you try the pattern without the forward slash? The other thought I'd have is whether there is something special to be done about the slash and dot since they may be seen as special characters within regular expressions.
Point 2: You do recognize that the dot is a special character in regular expressions, right? You may have to find a way to escape it so that the url likes like "/support/viewmatches\.aspx"

Resources