Rewrite Rule with Rewrite Map - iis

I got a problem with the creation of rewrite rule which is using a rewrite map in IIS. The goal is to rewrite requests like /Japan/test.html or /Chile/test.html to jp/en/simple-test and cl/en/simple-test.
Here is the rewrite rule:
<rule name="Rewrite sites" enabled="true" stopProcessing="true">
<match url="^/(.*)/test.html$" />
<conditions>
<add input="{Sites:{R:1}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}/en/simple-test" appendQueryString="false" />
</rule>
And this is my rewrite map:
<rewriteMap name="Sites">
<add key="chile" value="cl" />
<add key="japan" value="jp" />
</rewriteMap>
So once again, I want to dynamically translate all the languages into language codes and rewrite to language_code/en/simple-test. I want to use rewrite map as there will be more languages.

After few hours of tests, I was finally able to solve my problem. Here is how Rewrite Rule and Rewrite Map should look like for above conditions:
<rule name="Rewrite sites" enabled="true" stopProcessing="true">
<match url="(.*)/test.html$" />
<conditions>
<add input="{Sites:{R:1}}" pattern="(.*)" />
</conditions>
<action type="Rewrite" url="{C:1}/en/simple-test" />
</rule>
<rewriteMap name="Sites">
<add key="chile" value="/cl" />
<add key="japan" value="/jp" />
</rewriteMap>
Tip! If you don't known what is in {R:0}, {R:1}, {C:0} or {C:1} back referenced create fake rewrite action e.g.: <action type="Rewrite" url="www.test.com?a={C:1}" /> and in the a param you will see specyfic back reference content.

Related

IIS Rewrite with Rewrite Map

I'm struggling with IIS Rewrite maps. My map is simple:
<rewrite>
<rules>
<rule name="Redirect rule for MigratedSites" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.+)(/.+)" />
<conditions logicalGrouping="MatchAny">
<add input="{MigratedSites:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="http://tgt.sp.lab{R:1}" appendQueryString="true" redirectType="Temporary" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="MigratedSites">
<add key="/sites/apple" value="/sites/apple" />
<add key="/sites/banana" value="/sites/banana" />
</rewriteMap>
</rewriteMaps>
</rewrite>
What I have managed is http://src.sp.lab/sites/apple is redirected to http://tgt.sp.lab/sites/apple, and the same for banana, but what it doesn't do is redirect http://src.sp.lab/sites/apple/SitePages.Home.aspx to http://tgt.sp.lab/sites/apple/SitePages.Home.aspx
Anything I add after apple or banana causes the redirect to stop working. Any suggestions much appreciated.

How to translate this .htaccess line to web.config for IIS?

Here's my .htaccess rule that I confirm works:
RewriteRule ^([A-Za-z0-9-]+)/foo/products products.aspx?product_id=${product_map:$1} [NC, L]
What it does is uses the product_map.txt file to map between the product_id (query string) and the product name. For example let's say tools ID is 1. The following 2 would be equivalent/rewritten:
site.com/tools/foo/products
site.com/products.aspx?product_id=1
When using IIS's rule import, this turns into the following:
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^([A-Za-z0-9-]+)/foo/products" ignoreCase="true" />
<action type="Rewrite" url="products.aspx?product_id={product_map:{R:1}}" appendQueryString="false" />
</rule>
However it is saying the expression products.aspx?product_id={product_map:{R:1}} is not valid, is there any way to get this to work in IIS?
You need to use rewrite map in IIS. The work of it is same as product_map.txt, mapping the product name to product id.
<rewrite>
<rewriteMaps>
<rewriteMap name="product_map">
<add key="tools" value="1" />
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="rule">
<match url="^([A-Za-z0-9-]+)/foo/products" />
<conditions>
<add input="{product_map:{R:1}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="products.aspx?product_id={C:0}" />
</rule>
</rules>
</rewrite>

How to create a rewriteMap in IIS with dynamic path?

I'm trying to make a rewrite map in IIS to rewrite any call from domain.com/v/{PATH} to domain.com/api/v/{PATH} as i've changed my hosting logic and as an external service can't change the .GET request is making to domain.com/v/{PATH} in domain.com/api/v/{PATH} i have to rewrite any call in IIS..
I've created the following rewriteMap:
<rewrite>
<rewriteMaps>
<rewriteMap name="vmenuAuth">
<add key="/v/" value="/api/v/" />
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="Regola di reindirizzamento1 per vmenuAuth">
<match url=".*" />
<conditions>
<add input="{vmenuAuth:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
But it works only if the url has ONLY the /v/ so if i write domain.com/v/ it rewrite the url to domain.com/api/v/ but if i try to add the path domain.com/v/VHGbrbfFHHTRfbFKSZ it will still remain the same without redirecting the user to domain.com/api/v/VHGbrbfFHHTRfbFKSZ
It seems that it is unnecessary to apply the URL Rewrite Maps under the circumstance.
We will redirect the URL to the new path as long as we match the “/v/” segment, is it right?
Please refer to the below configuration.
<rewrite>
<rules>
<rule name="abc" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{Request_URI}" pattern="/v(.*)" />
</conditions>
<action type="Redirect" url="/api/v{C:1}" appendQueryString="false" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="MyMap" defaultValue="">
<add key="1234" value="HtmlPage1.html"></add>
</rewriteMap>
</rewriteMaps>
</rewrite>
Feel free to let me know if there is anything I can help with.

IIS Rewrite Maps with Wildcard for Querystring

Trying to figure out how to do wildcards for querystrings. Right now this works, but it has to be an exact match.
I have this rewrite rule.
<rule name="Redirect rule1 for Redirects ReSv">
<match url=".*" />
<conditions>
<add input="{Redirects:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="true" />
</rule>
My rewrite maps are like:
<rewriteMaps>
<rewriteMap name="Redirects">
<add key="/contact-us.aspx" value="/contact/" />
<add key="/contact-us/request-information.aspx" value="/contact/" />
</rewriteMap>
</rewriteMaps>
Trying to get something like this:
/contact-us.aspx?q=t&1=2
To redirect to:
/contact/
And include the querystring? so..
/contact/?q=t&1=2
Please us {URL} variable instead of {REQUEST_URI}. Then Rewritemap will be able to wildcard the query string.
<rule name="Redirect rule1 for Redirects ReSv" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{Redirects:{URL}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="true" redirectType="Permanent" />
</rule>

URL Rewrite Module IIS 7 redirect for urls with colon not working

I am trying to redirect the content of an old website to a new website using IIS. My rewrite map looks like this:
<rewriteMaps>
<rewriteMap name="Redirects">
<add key="/q/R.208-9899.aspx" value="/bla/garden/childrens-gardening/cat3376588.cat"/>
<add key="/store/dvd/lego-the-adventures-of-clutch-powers/8%3a939022" value="/bla/entertainment-books/dvd/cat12590010.cat" />
<add key="/store/dvd/world-steam-today--north-central-and-south-america/8:470831" value="/bla/entertainment-books/dvd/cat12590010.cat" />
</rewriteMap>
</rewriteMaps>
and my web.config looks like this:
<rule name="Redirect rule1 for Redirects" enabled="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{Redirects:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="http://www.example.com{C:1}" appendQueryString="false" />
</rule>
The first url works fine but the second two fail to work.
Any pointers would be useful.

Resources