Reputation: 226
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!!!
Upvotes: 0
Views: 4187
Reputation: 5205
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
Upvotes: 2
Reputation: 226
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.
Upvotes: 3