Simple trailing slash URL rewrite - iis-7

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>

Related

How to remove querystring when using Umbraco rewrite

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

IIS 7.0 URL REWRITE

I am setting up a redirect to WWW for one of our sites in the web.config and ran into a small issue. The code I have in the web.config for the rewrite is as follows :
<rewrite>
<rules>
<rule name="Redirect to www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="example.com" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:0}" />
</rule>
</rules>
</rewrite>
I'm finding that it's actually working a little too well. Because of the pattern "example.com", I'm seeing that it's now redirecting to our live site on dev and staging because our URLS are laid out like so : dev.example.com & staging.example.com. For the time being, I have just commented out the rewrite on these other web.configs but I'm wondering if there's a better pattern or option to get around this issue.
If you only want the root domain without subdomains then you should edit your pattern in the HTTP_POST section.
Place a ^ in front of the pattern which means start with. So If the url starts with example.com then it gets redirected to www.example.com.
If its dev.example.com, this rule will be ignored.
Edit your example:
<add input="{HTTP_HOST}" pattern="^example.com" />

IIS URL Rewrite module issue when trying to add trailing slash

My company launched a redesigned version of our site this past October. We have a normal ASP.NET domain setup, but some pages were renamed and moved to new locations, so we had to rely on the URL Rewrite module in Server 2012 to ensure that bookmarks were routing to the correct locations.
In most cases, a 1:1 URL match based on regex patterns or a rewrite map works fine and as you would expect. However, we have some legacy directories that I had to move over as well, that weren't a part of the redesign project. I moved these folders into a folder off of the root, something like:
$/sandbox/annualreport/
so that we can monitor and update all of the folders that haven't been redesgined in one place. The abridged rewrite entry I am using is this:
<rewrite>
<rules>
<clear />
<rule name="Sandbox URLs">
<match url="^(annualreport)\b/?(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Rewrite" url="sandbox/{R:1}/{R:2}" />
</rule>
</rules>
</rewrite>
This works fine for all URLs when the directories are followed by a trailing slash, like $/sandbox/annualreport/ but not when the trailing slash is left off, like $/sandbox/annualreport, even though the / is optional. The pages load and they are accessed correctly, but the problem is, some of these directories have local css, images, and scripts, which are accessed in the HTML like href="css/main.css" or src="scripts.js". When I view source and click on those style or js links, the rewrite looks for these files off of the root, unless the trailing slash is added.
This has become a nuisance because I have to have a 301 redirect for every "sandbox URL" (before this rewrite entry) to add the trailing slash. This works, but I have to specify every folder name in the regex:
<rule name="Custom Add Trailing Slash" stopProcessing="false">
<match url="^(annualreport)\b$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" url="{R:0}/" />
</rule>
I have tried the default URL Rewrite wizard for universally adding a trailing slash, which I would prefer, but it doesn't seem to fix the localized files problem:
<rule name="AddTrailingSlashRule1" stopProcessing="false">
<match url="(.*[^/])$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="{R:1}/" />
</rule>
How can I force a trailing slash to be added on a URL rewrite while maintaining the locally accessed files within the rewrite directories?
Any help would be greatly appreciated. Thanks.

IIS7 url rewrite issue when having "?"

I have a URL in the format
abc/pqr/xyz/?word1
and this needs to redirect to
abc/pqr/xyz/?word2
Is is possible to do using IIS7 rewrite?
It is possible using the IIS7 rewrite module with the following rule:
<rule name="Rewrite querystring" stopProcessing="true">
<match url="^abc/pqr/xyz/?$" />
<conditions>
<add input="{QUERY_STRING}" pattern="^word1$" />
</conditions>
<action type="Redirect" url="{R:0}?word2" appendQueryString="false" />
</rule>
What it does is: check that the url is abc/pqr/xyz/ or abc/pqr/xyz and the query string exactly word1. If yes, it redirects the user to the same url ({R:0}) but appending ?word2 instead.
It is important to have the appendQueryString="false" option as you don't want the module to append your word1 at the end.
By default, if not specified, the redirect is a 301 (permanent), that, regarding #Owen comment, seems to be the best fit for your case!
See here
You need to specify the url to match, " abc/pqr/xyz", then the query string to match and replace.

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.

Resources