url rewrite rule to parse url dynamically - url-rewriting

I am using rewrite map method to achive my seo friendly urls.
<rewriteMaps>
<rewriteMap name="Destinations">
<add key="/point-a-to-point-b" value="/destination-final.asp?from=point%20a&to=point%20b" />
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="Rewrite rule1 for Destinations">
<match url=".*" />
<conditions>
<add input="{Destinations:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}" appendQueryString="false" />
</rule>
</rules>
My problem is I have too many destinations and having to write a map for each results in 500 error. I believe there is a limit to the number of maps you can have on single web.config file.
would it be possible to achive this using a wildcard rule?
that would parse the url and use the bit before "to" as starting point and the bit after "to" as end point and send it to the file as querystring?
for example
/newyork-to-texas
would send the following querystring:
from=newyork&to=texas

Could you please share a detailed error message for 500 error. if you want to achieve your requirement /newyork-to-texas to from=newyork&to=texas you could use below url rewrite rule:
<rule name="send value to query string" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{REQUEST_URI}" pattern="(.*)/(.*)\-to\-(.*)" />
</conditions>
<action type="Redirect" url="http://localhost:746/default.aspx?from={C:2}&to={C:3}" appendQueryString="false" />
</rule>
Regards,
Jalpa.

Related

URL Rewrite rule in .NET to force www but ignore http(s)

I'm looking to set up a rewrite rule in my web.config file which forces URLs to use the 'www' sub-domain. This is done like so:
<rules>
<rule name="Add WWW" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?!www\.)(.*)$" />
</conditions>
<action type="Redirect" url="http://www.{C:0}{PATH_INFO}" redirectType="Permanent" />
</rule>
However, the project is running multiple websites on multiple domains, some of which use SSL and some don't.
The code above hard-codes http:// into the redirect. What I'm looking to do is to abstract this such that the http or https protocols are maintained during the redirect without being hard-coded.
Some examples of the desired results are:
http://olddomain.com > http://www.newdomain.com
https://someolddomain.com > https://www.somenewdomain.com
Many thanks.
I found a reference which explains one approach in doing this. It uses rewrite key/value pairs as a 'variable' in the action url, as (as far as I can find out) there's no way currently to grab the protocol as a native variable inside the rule.
The rule has been modified as follows, which should do what's required:
<rewrite>
<rules>
<rule name="Add www maintain https" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?!www\.)(.*)$" />
</conditions>
<action type="Redirect" url="{MapSSL:{HTTPS}}www.{C:0}{PATH_INFO}" redirectType="Permanent" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="MapSSL" defaultValue="http://">
<add key="ON" value="https://" />
<add key="OFF" value="http://" />
</rewriteMap>
</rewriteMaps>
</rewrite>
There's also another reference which includes this amongst alternative ways to achieve the same thing.

IIS rewrite rule to get domain without tld

Looking to create a working IIS rewrite (not a redirect) rule to rewrite http{s}://www.client1.com/page1.htm to https://client1.mysite.com/myapp/client1/page1.htm - I just can't get it working. Driving me a little mad.
My main problem is getting the domain name WITHOUT the TLD part returned as an R{x} or C{x} parameter...
Note the client name is in two places in the target url - AND Note that the original url could be client2.com or client3.org or client4.net
This rule will rewrite it for you:
<rules>
<rule name="Rewrite clients domains" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)?([a-zA-Z0-9\-\.]+)\.(\w+)$" />
</conditions>
<action type="Rewrite" url="{MapProtocol:{HTTPS}}://{C:2}.mysite.com/myapp/{C:2}/{R:0}" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="MapProtocol">
<add key="on" value="https" />
<add key="off" value="http" />
</rewriteMap>
</rewriteMaps>

Ignore match in web.config rewrite rule

I have and issue where a rule in the web.config is catching something I want it to ignore.
I have:
http://domain.com/video-conferencing-room/australia/australian-capital-territory/barton
Getting picked up by the below rule. I've added part hoping that would fix it, but no joy. The routing for that
<rule name="video-conferencing-room-country">
<match url="^video\-conferencing\-room/([0-9a-z- ]+)" />
<conditions>
<add input="{HTTP_HOST}" matchType="Pattern" pattern="^video\-conferencing\-room/([0-9a-z- ]+)/([0-9a-z-, ]+)/([0-9a-z-, ]+)" ignoreCase="true" />
</conditions>
<action type="Redirect" url="/meeting-rooms/{UrlEncode:{R:1}}" redirectType="Permanent" />
</rule>
but this condition is stopping urls like this:
http://domain.com/video-conferencing-room/australia
from hitting the rule.
I updated to use {URL}, and :
http://domain.com/video-conferencing-room/australia
now works,
but I still have an issue that
http://domain.com/video-conferencing-room/australia/australian-capital-territory/barton
Is getting rewritten by this rule:
<rule name="video-conferencing-room country state">
<match url="^video\-conferencing\-room/([0-9a-z- ]+)/([0-9a-z-, ]+)" />
<conditions>
<add input="{URL}" matchType="Pattern" pattern="^video\-conferencing\-room/([0-9a-z- ]+)/([0-9a-z-, ]+)/([0-9a-z-, ]+)" ignoreCase="true" negate="true" />
</conditions>
<action type="Redirect" url="/meeting-rooms/{UrlEncode:{R:1}}/{UrlEncode:{R:2}}" redirectType="Permanent" />
</rule>
When I would like it ignored.
Do you know what I'm doing wrong?
Your condition is not correct, you are setting it with {HTTP_HOST} which is only the host name part, namely in your example domain.com.
Instead try using the {URL} as the input which will include the URL including the video-conferencing... parts.
Try adding $ at the end
^video\-conferencing\-room/([0-9a-z- ]+)/([0-9a-z-, ]+)$
and you can remove the condition. $ signifies "end of string".

How to Filter urls with URL rewrite rules

<rewrite>
<rules>
<rule name="HTTP to HTTPS redirect" enabled="true" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTPS}" pattern="OFF" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Found" />
</rule>
</rules>
</rewrite>
I have used above url rewrite rule in my web.config file to enable SSL for the whole site.
Now I need to change the above rule to filter 2 urls which should work as http.
Let's say those urls as https://www.domain.com/owner/Marketing and https://www.domain.com/owner/getinfo.
Currently those urls are https hence above rules.So how can I change above rule to filter above 2 urls (i.e. http://www.domain.com/owner/Marketing and http://www.domain.com/owner/getinfo )?
I would add a separate rule to exclude urls that should not be redirected to https.
I cannot test it right now, as I don't have an IIS7 by the hand. but give it a try:
<rewrite>
<rules>
<rule name="Skip HTTPs" enabled="true" stopProcessing="true">
<match url="^(Marketing|getinfo)" ignoreCase="true" />
<conditions>
<add input="{HTTPS}" pattern="OFF" />
</conditions>
<action type="None" />
</rule>
<rule name="HTTP to HTTPS redirect" enabled="true" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTPS}" pattern="OFF" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Found" />
</rule>
</rules>
</rewrite>
Note that you have to put that file into /owner/ directory as per your example.
you may have seen the documentation on that already - just pointing it out where i found it:
http://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module
Also here is a maybe-duplicate of that question using lookahead regex rules: Rewriting URLs in IIS7

URL Rewrite - Redirect to different port and changing URL using map

I want to rewrite URL to redirect to different port, based on HTTP_URL while preserving rest of URL and query string (if specified).
For example,
http://host/john/page.aspx should be redirected to http://host:1900/page.aspx,
http://host/paul/anotherpage.aspx?query to http://host:1901/anotherpage.aspx?query
and http://host/ringo to http://host:1902/
I've added bunch of rules for every allowed port, but it does not look efficient or manageable.
I'm trying to employ map, (ie john->1900, paul->1901) but cannot figure out how to assemble desired URL.
Any suggestions?
It took some fiddling to get it working but looking back at it the solutions is quite simple and elegant.
<rewrite>
<rules>
<clear />
<rule name="Redirect known names to ports" stopProcessing="true">
<match url=".*" />
<conditions trackAllCaptures="true">
<add input="{REQUEST_URI}" pattern="/(.*?)/(.*)" />
<add input="{NameToPort:{C:1}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}:{C:3}/{C:2}" appendQueryString="false" redirectType="Permanent" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="NameToPort">
<add key="john" value="1900" />
<add key="paul" value="1901" />
<add key="ringo" value="1902" />
</rewriteMap>
</rewriteMaps>
</rewrite>
Let me know if this is what you were looking for.

Resources