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

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>

Related

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.

exclude a URL from rewrites

I have a nodejs application running on iis.
I have url rewrite rule which sends all request to a custom server.js page.
I want to add a new rule or modify the existing one so that a particular url /something reaches the iis and not the rule I have written.
My rule currently:
<rewrite>
<rules>
<rule name="node">
<match url="/*" />
<action type="Rewrite" url="server.js" />
</rule>
</rules>
</rewrite>
Please help on this one.
<rule name="sendToNode">
<match url="/*" />
<conditions>
<add input="{URL}" pattern="/something$" negate="true" />
</conditions>
<action type="Rewrite" url="server.js" />
</rule>

Rewrite Rule with Rewrite Map

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.

IIS7 Url Rewrite module doesn't work

I am tring set the rule as below:
<rewrite>
<rules>
<rule name="Test" stopProcessing="false">
<match url="abcd.com/admin(.*)" />
<action type="Redirect" url="xyz.com/admin{R:1}" logRewrittenUrl="true" />
<conditions trackAllCaptures="true">
</conditions>
</rule>
</rules>
</rewrite>
When I try to access to url:abcd.com/admin/login but action not work. Please help me about that. Thanks!
The match url does not contain the domain, so you need to remove that:
<match url="^admin(.*)" />

IIS rewrite change filename

I have some old URLs in a forum application points to http://mydomain.tld/showthread.php?p=xxxx where xxxx is the an integer id.
My new forum application uses viewtopic.php?p=xxxx and I want to handle this using IIS web.config rewrite rules.
I have tried to add the last rule as the following:
<rewrite>
<rules>
<rule name="Redirect www to non www" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.*)" />
<action type="Redirect" url="http://domain.tld/{R:1}" />
<conditions>
<add input="{HTTP_HOST}" pattern="^domain\.tld" negate="true" />
</conditions>
</rule>
<!-- this for change from showthread.php to viewtopic.php -->
<rule name="Rewrite" stopProcessing="true">
<match url="(.*)showthread\.php" />
<action type="Rewrite" url="viewtopic\.php" appendQueryString="true" />
</rule>
</rules>
</rewrite>
However, it does not able to redirect to the viewtopic.php. Any suggestion?
I have to enable httpRedirect in the IIS. Then I used this redirect rule in the web.config at the root of my forum:
<httpRedirect enabled="true" exactDestination="true" httpResponseStatus="Found">
<add wildcard="*showthread.php" destination="/viewtopic.php?$P" />
</httpRedirect>

Resources