IIS rewrite rule not working - iis

I would like to rewrite this:
domain.com/gallery/123123/name-of-the-image/
To display content from here: domain.com/gallery/showImage/name-of-the-image-123123
Here's my current code:
<rule name="Gallery - Single image">
<match url="^gallery/([0-9])/([a-z-/]+)/?$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Rewrite" url="gallery/showImage/{R:2}-{R:1}" />
</rule>
How should I alter the rewrite code to make it work?
Big thanks in advance

Your regex to match the URL should be:
^domain\.com\/gallery\/(\d+)\/([a-z-]+)\/?$
and the rewrite URL should be:
domain.com/gallery/showImage/{R:2}-{R:1}

Related

IIS rewrite rule not triggered when file-name plus extension is in the url

I am struggling getting an IIS rewrite rule working.
The old urls look like:
https://wwww.testserver.com/package.aspx?TrackingNumber=number
The new web app is expecting the urls in this format:
https://wwww.testserver.com/package/number
Currently I have the following rule setup, which is not working.
<rule name="Rewrite to new Package site" stopProcessing="true">
<match url="^package\.aspx\?TrackingNumber=([_0-9a-z-]+)" />
<action type="Redirect" url="https://www.testserver.com/package/{R:1}" logRewrittenUrl="true" />
</rule>
The interesting part is, if I remove package.aspx\? from the rule, urls like these https://wwww.testserver.com/oldwebsite/TrackingNumber=number are getting matched.
In my test calls I replaced package.aspx with package.html and these urls are not getting matched as well. It looks like IIS ignores urls with filenames in the url.
You could use below url rewrite rule to redirect https://wwww.testserver.com/package.aspx?TrackingNumber=number to https://wwww.testserver.com/package/number:
<rule name="test" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{REQUEST_FILENAME}" pattern="package(.aspx)" />
<add input="{QUERY_STRING}" pattern="TrackingNumber=(.*)" />
</conditions>
<action type="Redirect" url="http://localhost:2568/package/{C:1}" appendQueryString="false" />
</rule>
Note: You could modify hostname as per your requirement.
Regards,
Jalpa

IIS URL Rewrite rule to replace part of the URL

I am new to the IIS Rewrite rule and trying to create a rule to replace part of url
e.g.www.abc.com/assets/global/xyz.jpg
should redirect to www.abc.com**/en/globalassets/**assets/global/xyz.jpg
I fiddle around with the following rule but no success
<rule name="url replace">
<match url="^(.com/assets/global/)" />
<action type="Rewrite" url=".com/en/globalassets/assets/global/{R:2}" />
</rule>
According to your description, I have tested on my side , you could use urlrewite rule as below:
<rule name="rule1" enabled="true" stopProcessing="true">
<match url="assets/global/(.*)" />
<conditions>
<add input="{REQUEST_URI}" pattern="en/globalassets" negate="true" />
</conditions>
<action type="Redirect" url="http://{domain}/en/globalassets/assets/global/{R:1}" />
</rule>
Firstly, we couldn't add value like **.com in match url, because this part could only catch path of the url.
You could see this is a url structure:
http(s)://httphost/path?querystring.
You could only get it in conditions tag but not pattern.
Then you should add condition to check the request URL match "en/globalassets" or not to avoid running redirect rule once and once again.

How to pass two URL parameters with IIS 8.5 URL ReWrite module?

I am trying to pass two parameters in the URL with the following url rewrite rule and the page keeps showing 404 error.
Actual URL: https://devbox.mysite.com/kb/article/?slug=view-my-class-schedul&role=56c4cfe091121c0b5b47fe66
I want to show URL on browser: https://devbox.mysite.com/kb/article/view-my-class-schedule
Current URL Rewrite rule that's not working:
<rule name="KB-rewrite" enabled="true">
<match url="^kb/article/([a-zA-Z0-9\-]+)&role=([a-zA-Z0-9\-]+)$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{QUERY_STRING}" pattern="1" negate="true" />
</conditions>
<action type="Rewrite" url="kb/article/?slug={R:1}&role={R:2}" appendQueryString="false" />
</rule>
Okay, I think I figured it out. The display URL has to be something like: https://devbox.mysite.com/kb/article/view-my-class-schedule/reader. I guess the way I was thinking is not possible.

Remove a part of url with rewrite

I'm not familiarized with this feature and I've found some examples how to do something but I don't know how to set this case:
I want this url http://www.domain.co.uk/anything/en to become http://www.domain.co.uk/anything/
I want to remove the final 'en'
I've tried with:
^/([_0-9a-z-]+)/([_0-9a-z-]+)/en
/{R:1}/{R:2}
or
^http://www.domain.co.uk/([_0-9a-z-]+)/(en)
http://www.domain.co.uk/{R:1}/
But it doesn't work.
You can use the following rule:
<rule name="Remove en" stopProcessing="true">
<match url="^(.*)/en$" />
<action type="Rewrite" url="{R:1}" />
</rule>
It will match any url ending with /en and remove this part.
From your example, http://www.domain.co.uk/anything/en will be rewritten as http://www.domain.co.uk/anything
If you want the user to be redirected, then use the following rule:
<rule name="Remove en" stopProcessing="true">
<match url="^(.*)/en$" />
<action type="Redirect" url="{R:1}" />
</rule>
The type="Redirect" with no option triggers a permanent(301) redirect.

How to rewrite a querystring with URL Rewrite

Please I am just starting off with using iis rewrite module and I have a task to rewrite a url with querystring.
What I have to do is rewrite this url
http://www.blah.com/Pages/Content.aspx?id=n%2fWA2PJCJXXnu8mvdC0jXQ%3d%3d&ln=Gy7UlI4cSJE94Wa2qudbFQ%3d%3d
to something like this http://www.blah.com/Pages/Content.aspx?id=Ecommerce
Hello Mathew thanks for the link, however I am not really up to speed with url rewrite module I have come with this and it doesn't seem to be working
<rules>
<rule name="QueryStringRW" stopProcessing="true">
<match url="^Content\.aspx$" ignoreCase="true" />
<conditions>
<add input="{QUERY_STRING}" pattern="id=(\d+)" />
<add input="{IDtoTitleMap:{C:1}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="Content.aspx?id={C:1}" appendQueryString="false" redirectType="Permanent"/>
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="IDtoTitleMap">
<add key="id=n%2fWA2PJCJXXnu8mvdC0jXQ%3d%3d&ln=Gy7UlI4cSJE94Wa2qudbFQ%3d%3d" value="Id=Ecommerce" />
</rewriteMap>
</rewriteMaps>
A rewrite rule requires the following pieces of information:
Name of the rule
Pattern to use for matching the URL string
Optional set of conditions
Action to perform if a pattern is matched
A detailed approach on how to creating rules for the URL Rewrite Module is available at: http://learn.iis.net/page.aspx/461/creating-rewrite-rules-for-the-url-rewrite-module/

Resources