URL Rewrite troubleshoot - url-rewriting

We have our URL rewrite as followed. I tested the pattern in IIS 8.5 URL Rewrite and the patterns matched as what we wanted. However, it's 404 every single time. Will someone take a look and see what is wrong with my URL rewrite? Thank you.
URL: devbox.mysite.com/article/how-to-use-onedrive
Pattern: (.*)/article/(.*)$
Action Rewrite URL: {R:1}/article/?artID={R:2}
Currently the page shows 404. Below is the XML code in web.config file.
<rule name="Article-rewrite" enabled="true">
<match url="(.*)/article/(.*)$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Rewrite" url="{R:1}/article/?artID={R:2}" appendQueryString="false" />
</rule>

Try this:
<rule name="Article-rewrite" enabled="true">
<match url="article/(.*)$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Rewrite" url="article/?artID={R:1}" appendQueryString="false" />
</rule>
I've removed the domain from the match url, as you're not switching the domain.

Related

Need to redirect folder URL /media/default to Page URL /media/default/image

IIS rewrite rule is not working.
I have tried adding the following rule for redirect but it's not working. Can anyone help me on this as I am not familiar with rewrite rules.
<rule name="test" patternSyntax="ExactMatch" stopProcessing="true">
<match url='/media/default' />
<action type="Redirect" url="/media/default/image" />
<conditions>
</conditions>
</rule>

url rewrite rule to parse url dynamically

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.

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>

IISRewrite encoding of %2f in path

So I have a path like this (I am using webforms):
folder/first/second/
which has the following rewrite rule behind it
<rule name="firstLevel">
<match url="^folder/([^/]+)/([^/]+)/?$" />
<action type="Rewrite" url="firstSecond.aspx?param1={R:1}&param2={UrlEncode:{R:2}}" />
</rule>
my issue is when I try and pass the following:
folder/first/sec%2fond/ (I want R2 to be sec%2fond where I will urlDecode it back to the page. for a result like "sec/ond".
However my rule keeps taking it as though I want
folder/first/sec/ond
Requested URL
http://localhost:85/research/first/sec/ond/
Physical Path
\rewritetest\folder\first\sec\ond\
Thanks for any assists..
I found a working solution to this issue for anyone else.
<rule name="secondLevel">
<match url="^folder/(.+)/(.+)/?$" ignoreCase="true" />
<action type="Rewrite" url="firstsecond.aspx?param1={C:1}&param2={C:2}" />
<conditions>
<add input="{UNENCODED_URL}" pattern="/folder/([^/]+)/([^/]+)/?$" />
</conditions>
</rule>

How do I ignore the .aspx extension when using a rewrite map?

I have a rewrite map with ~100 old URL to new URL rows:
<rewriteMaps>
<rewriteMap name="OldSiteRewriteMap" defaultValue="">
...
<add key="/a/b/oldpage" value="/x/y/newpage" />
...
</rewriteMap>
</rewriteMaps>
I have a rewrite rule that uses that map to redirect URL's (we've just launched a new website and we're mapping the URL's from the old site to the corresponding URL on the new site):
<rule name="Redirect old site URLs">
<match url=".*" />
<conditions>
<add input="{OldSiteRewriteMap:{PATH_INFO}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" redirectType="Permanent" appendQueryString="false" />
</rule>
When I visit http://example.com/a/b/oldpage (the old site URL) I get redirected to http://example.com/x/y/newpage - great! But when I visit http://example.com/a/b/oldpage.aspx -note the .aspx extension - I don't get redirected.
How can I tell my rewrite rule to ignore the ".aspx" extension when it's trying to map URL's using the rewrite map?
A coworker suggested the following - using a regex in the "match url" to capture everything except a possible trailing ".aspx" and then passing the capture to the rewrite map in lieu of PATH_INFO or any of the other server vars. Seems to work pretty well.
<rule name="Redirect old site URLs">
<match url="^(.+?)(\.aspx)?$" />
<conditions>
<add input="{OldSiteRewriteMap:{R:1}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" redirectType="Permanent" appendQueryString="false" />
</rule>

Resources