How to remove querystring when using Umbraco rewrite - asp.net

I have a rewrite rule as below to my Umbraco/ASP .Net site
<add name="Test" virtualUrl="^~/category/(.*)" destinationUrl="/category?cat=$1" rewriteUrlParameter="ExcludeFromClientQueryString" ignoreCase="true" />
The idea is if a user types
www.example.com/category/electronics
www.example.com/category/devices
www.example.com/category/food
www.example.com/category/beverages
Everything works as i would expect when they reach the page without any rules and plain URLs i.e.
www.example.com/category?cat=electronics
If i have the above rule it enabled it always adds/forwards the querystring parameter i.e cat= when you click a link on any of the pages above.
How could i stop forwarding/adding the parameter?

You could use below URL rewrite rule:
<rule name="add query string" stopProcessing="true">
<match url="^category/([^/]+)/$" />
<action type="Redirect" url="http://www.sample1.com/category?cat={R:1}" appendQueryString="false" />
</rule>
if you did not install URL rite module in iis you could install it from the below link:
https://www.iis.net/downloads/microsoft/url-rewrite

Related

How to redirect a url only with specific parameters in IIS

I have a url that looks like this:
www.mywebsite.com/page.aspx?code=1.a
I want to redirect this URL via IIS to:
www.mywebsite.com/page.aspx?code=1.b
I would like to do this through IIS and not within the code.
I have various other website urls that look like:
www.mywebsite.com/page.aspx?code=2
www.mywebsite.com/page.aspx?code=3.a
www.mywebsite.com/page.aspx?code=6.c
I don't want these to be affected.
Thanks.
An easy way to do this in IIS 7.5 is to install the URL Rewrite extension from Microsoft into IIS.
http://www.iis.net/downloads/microsoft/url-rewrite
Once you have it installed, you can just add a rule to redirect from ?code=1.a to ?code=1.b. In IIS, you'll see a new entry called URL Rewrite under the IIS heading for your website. You can use the editor there to create a new rule. Once the rule is created, it will be written into your web.config file.
In the web.config file, the rule should look something like this:
<system.webServer>
...
<rewrite>
<rules>
<rule name="Code=1.a redirect" patternSyntax="ExactMatch"
stopProcessing="true">
<match url="page.aspx" />
<action type="Redirect" url="page.aspx?code=1.b"
appendQueryString="false" redirectType="Permanent" />
<conditions>
<add input="{QUERY_STRING}" pattern="code=1.a" />
</conditions>
</rule>
</rules>
</rewrite>
...
</system.webServer>

Simple trailing slash URL rewrite

I've never done URL rewriting (redirecting). I have a website http://sub.sub.domain.ext/app/. "app" signifies an "Application", not a virtual directory.
When a user navigates to http://sub.sub.domain.ext/app (no slash) I need the IIS 7 to redirect him to the URL with the trailing slash.
The thing is that I want the rule to be applied only when the user navigates to application. I don't want the trailing slash to be appended to every filename.
I have tried modifying the predefined rule in IIS7 manager but with no success. I've tried exact matching the whole URL, constraining the conditions, or simply using the original predefined rule. But even when using the original rule, it rewrites all subsequent request files/dirs/URLs, but it does not redirect the user from http://sub.sub.domain.ext/app to http://sub.sub.domain.ext/app/.
The rule you are looking might be as simple as:
<rule name="Add trailing slash" stopProcessing="true">
<match url="^app$" negate="false" />
<action type="Redirect" url="{R:0}/" />
</rule>
The url="^app$" pattern matches only the url http://www.yourwebsite.com/app and nothing else.
If you need to limit this behavior to the host sub.sub.domain.ext, you can add a condition:
<rule name="test" stopProcessing="true">
<match url="^app$" negate="false" />
<action type="Redirect" url="{R:0}/" />
<conditions>
<add input="{HTTP_HOST}" pattern="^sub.sub.domain.ext$" />
</conditions>
</rule>

Rewrite Subfolder to Subdomain in web.config

I'm attempting to write a rewrite rule for the following scenario.
User attempts to load this picture:
domain.com/images/folder/picture.jpg
and instead, I need it to load:
cdn.domain.com/images/folder/picture.jpg.
Here's what I have that isn't working:
<rule name="CDN rewrite for Images">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="domain.com" />
<add input="{REQUEST_URI}" pattern="^/images/folder/(.*)$" />
</conditions>
<action type="Rewrite" url="cdn.domain.com/images/folder/{C:1}" />
</rule>
UPDATE: Adding additional info. Most pictures are being served up from Joomla so while the root of the domain is something like domain.com, most images are input with a src="/images/folder/picture.jpg" Not quite sure how this is affecting the rewrite, but none of the options on cheesemacfly's answer below, are working...
UPDATE2: While cheesemacfly was unable to help me in my particular circumstances, I awarded him the bounty and marked his answer as the accepted one because he went above and beyond to try to help me in chat. Hopefully his answer will help someone with rewrites on IIS.
EDIT:
To be able to rewrite (and not only redirect) urls to outside websites, you need to install the Application Request Routing module and enable the proxy mode.
To do so:
Download and install the module
Open your IIS management console (inetmgr)
Select Your server node
Double click on Application Request Routing Cache:
Click on Server Proxy Settings on the Actions pane (right of the screen)
Check the box Enable proxy and click on Apply
The second step is about setting up your rules.
If you want your rewrite to be based on the path then use the following code:
<rewrite>
<rules>
<rule name="Rewrite to cdn domain">
<match url="^images/folder/(.+)$" />
<action type="Rewrite" url="http://cdn.domain.com/images/folder/{R:1}" />
</rule>
</rules>
</rewrite>
Or if you keep the same folder architecture on the second website you can simplify as follow:
<rewrite>
<rules>
<rule name="Rewrite to cdn domain">
<match url="^images/folder/(.+)$" />
<action type="Rewrite" url="http://cdn.domain.com/{R:0}" />
</rule>
</rules>
</rewrite>
If you want to catch only the files ending with a specific extension (let's say images):
<rewrite>
<rules>
<rule name="Forward to cdn domain">
<match url="^images/folder/.+\.(?:jpg|bmp|gif)$" />
<action type="Rewrite" url="http://cdn.domain.com/{R:0}" />
</rule>
</rules>
</rewrite>
Please refer to: http://www.iis.net/learn/extensions/url-rewrite-module/iis-url-rewriting-and-aspnet-routing (section "Which Option Should You Use?")
TIP:
The best way to test your pattern is to use the IIS test pattern tool.
At the root of your website -> URL Rewrite -> Create a blank rule -> click on test pattern:
If you don't get the expected result, you can debug your rewrite using the Failed Request Tracing tool
NOTE: Changing the rule to be a redirect instead of a rewrite fixes the problem. Ideally you want it to be a redirect but I have spent many hours trying to get the rewrite to work, and so far no solutions yet.
<rule name="Rewrite to images.cdn.com" enabled="true" stopProcessing="true">
<match url="images/(.+)$" ignoreCase="true" />
<action type="Redirect" url="http://images.cdn.com/{R:1}" />
</rule>

IIS7 Url Rewrite - Why does Redirect work and Rewrite does not?

What I want to do is rewrite subdomains to the main application, and append the specified subdomain onto the query string. For instance, "http://a.main.com" should rewrite to "http://www.main.com/default.aspx?SD=a".
Here is my rewrite rule:
<rule name="SubDomain" stopProcessing="true">
<match url="^$" />
<conditions trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^([A-Za-z0-9]+)\.main\.com$" />
</conditions>
<action type="Rewrite" url="http://www.main.com/default.aspx?SD={C:1}" logRewrittenUrl="false" />
</rule>
When I navigate my browser to "http://a.main.com", I get a 404. However, when I change the rule to be a redirect rule instead, it redirects correctly. The fact that it works when set to redirect mode, but not when set to rewrite mode confuses me greatly. What's going on?
FYI my HOSTS file is set up so that www.main.com and a.main.com both point to 127.0.0.1. The web site's only binding in IIS7 has its Host Name property set to 127.0.0.1.
The "http://www.main.com/" portion of the node's url property needed to be removed. Here's what it looks like now:
<action type="Rewrite" url="default.aspx?SD={C:1}" logRewrittenUrl="false" />
This works.

HTTP Error 404.4 - Not Found The resource you are looking for does not have a handler associated with it

I've hosted a site in IIS but whenever I browse to the site I get 404.4. How can I solve this? I've referred several posts and they all say the issue is about staticfile but it is already mapped. What more can I do? Here is the attached picture of handler mappings in my iis 7.0
Any ideas?
EDIT:
I have this url rewriter set up:
<rules>
<rule name="Imported Rule 1-1" enabled="true" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{SERVER_PORT}" pattern="80" />
</conditions>
<action type="Rewrite" url="https://abc.com/{R:1}" />
</rule>
When I disable this rule the http:// request is correctly handled. But when I enable it, I get this error.
Yet another update:
If I replace this:
<action type="Rewrite" url="https://abc.com/{R:1}" />
with
<action type="Redirect" url="https://abc.com/{R:1}" />
It all works out pretty well.
I was having the exact same issue. I installed Application Request Routing component and then set the Proxy enabled and selected the Use URL Rewrite to inspect incoming requests entered the redirect url alias and mine worked

Resources