Replace a word using IIS URL Rewrite - iis

I am using the IIS Rewrite module with my web.config, and would like to rewrite certain requests by replacing a word in the URL.
Example: http://domain.com/windows to be http://domain.com/WINDOWSSoftware
I'm aware that a URL with query string parameters can be rewritten using for example below rule
<rules>
<rule name="Rewrite to article.aspx">
<match url="^article/([0-9]+)/([_0-9a-z-]+)" />
<action type="Rewrite" url="article.aspx?id={R:1}&title={R:2}" />
</rule>
</rules>
Instead of /article?id=243&title=some-title we can have /article/234/some-title
I'm wondering if this can be achieved using URL Rewrite, or can it be done by developing a custom provider.

This can definitely done with url rewrite module with bit of regex pattern matching.
http://domain.com/windows/sdfsdf
so if you just match {R:1} or {C:1} depending on how you setup
The final redirect can look like
{HTTP-HOST}/WindowsSoftware/{R:2}

Related

Need to redirect few URLs via web.config and excluding the rest

We have a requirement for redirecting couple of webpages to different URLs of our website. For instance:
https://old-domain/-->https://new-domain/;
https://old-domain/promotion/campaign-->https://new-domain/;
https://old-domain/about-us-->https://new-domain/about-us/company;
https://old-domain/about-us/awards-and-recognitions-->https://new-domain/about-us/company;
https://old-domain/careers-->https://new-domain/careers/;
https://old-domain/careers/apply-for-a-job-->https://new-domain/careers/;
https://old-domain/claim-status-->https://new-domain/;
https://old-domain/contact-us-->https://new-domain/contact-us;
We need generic rewrite rules for the above, where
we either redirect to the desired destination URLs (manually hard-coding them in the rewrite rules)
or
add a single redirect rule for the first URL and regex expression to skip the rest (since the rest can be managed internally within the CMS but not the first)
I would do it via IIS Rewrite Rules in the web.config (unless the client requires to be able to edit those in the UI)
I would add a rule for each Url (or maybe combine a few where possible)
They could look like this:
<rewrite>
<rules>
<rule name="Redirect old domain" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="[www.]oldDomain.com$" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://www.newDomain.com/{R:0}" />
</rule>
....
</rules>
</rewrite>
Edit: Updated the rule above so that it will cater for any page on the old domain and will redirect to the same page of the new domain.
Examples:
www.olddomain.com will redirect to www.newdomain.com
www.olddomain.com/about-us will redirect to www.newdomain.com/about-us
Now, you can create the /about-us page in Sitefinity and make it a redirect page and redirect to wherever you want.

IIS URL Rewriting - Change a querystring key and value into a route

I've recently implemented IIS URL Rewriting on one of my websites. I'm using URL Redirection rather than rewriting for everything. All of my static redirections are working perfectly, however, there's one specific type of dynamic redirection that I can't seem to make work.
I had old URL's that looked like this:
http://example.com/?tag=mytag
I'd like this to be redirected to the new URL format of:
http://example.com/tag/mytag
For these URL's the querystring key (tag) is known and fixed, however, the querystring value ("mytag" in the example above) is entirely dynamic and unknown in advance (so I don't believe it's therefore possible to use IIS Rewrite Maps).
Is is possible to add an IIS Rewrite rule that performs this kind of redirection for all possible querystring values that may be supplied?
Yes, the guts of the solution are below. Whats going on is...
1st condition means only apply this rule to the top level of the site. So http://example.com/?tag=mytag will redirect, whereas http://example.com/foobar/?tag=mytag wouldnt.
2nd condition is the magic. It only runs if a query param called tag exists, and the (.*) is a regex to grab the value for use in the new URL.
The action uses the value you grabbed in the 2nd condition referenced as {C:1}. appendQueryString does exactly what it says - set as appropriate. redirectType should be left as Temporary (HTTP response code 307) until your happy, then change it to Permanent (HTTP response code 301). Once you send a 301 response the client(/search engine) will potentially cache the response and not re-request from the server causing problems if you make a mistake.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect based on tag query value">
<conditions>
<add input="{REQUEST_URI}" pattern="$" />
<add input="{QUERY_STRING}" pattern="tag=(.*)" />
</conditions>
<action type="Redirect" url="tag/{C:1}/" appendQueryString="false" redirectType="Temporary" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Custom URL Rewriting in Classic ASP

I have been trying to rewrite the URL in classic ASP. I am currently using IIS 7.5. I tried to use the URL Rewrite plugin which converts the following link:
http://blog.johnavis.com/blog/default.asp?id=19
into something like this:
blog.johnavis.com/19/
blog.johnavis.com/id/19/
blog.johnavis.com/blog/default/19
blog.johnavis.com/blog/default/id/19
I want to convert into something like this:
http://blog.johnavis.com/blog/myblog/
Can that be achieved? Any help would be appreciated.
Basically all the rewrite module does is to edit your web.config file. You're probably better off editing the file yourself. You'll find that it has created a section called rewrite, add the following rule
<rewrite>
<rules>
<rule name="My Blog">
<match url="blog/myblog/" />
<action type="Rewrite" url="blog/default.asp?id=19" />
</rule>
</rules>
</rewrite>
You can achieve this by creating URL rewrite rule in IIS Manager by defining a pattern. Use the following links to learn about that.
http://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module
http://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-video-walkthrough
Hope this helps...

Rewriting to an URL with hashtags

I'm using IIS URL rewriting module to mask my internal URLs with friendly URLs through rewrite maps and Rewrite rules (not redirection). This is my rewrite map:
<rewriteMap name="HashTest">
<add key="/nohash" value="/nohash.aspx" />
<add key="/hash1" value="/hashtest.aspx#hash1" />
</rewriteMap>
and this is my rewrite rule:
<rule name="Rewrite rule1 for HashTest">
<match url=".*" />
<conditions>
<add input="{HashTest:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}" />
</rule>
This is working for URLs with no hashtags, so every time I query www.mysite.com/nohash it shows me content from www.mysite.com/nohash.aspx with our changing the URL on the browser.
Now when I try to rewrite to a URL containing a hashtag I get a 404 error, for instance www.mysite.com/hash1 should just show me content from /hashtest.aspx#hash1 but I just get a 404.
Now if I change my rule action type to Redirect it does make redirection successfully, so I don't know why it doesn't work with rewrite.
I know hashtags are not sent to the server on the request, but it would make sense if my rewrite map was backwards, like <add key="/hashtest.aspx#hash1" value="/hash1" />.
Any insights on why redirection works with hastags but rewrite doesn't?. I'm not married to IIS redirection, if you have another module or approach I can use it's very welcome
The part after the hash sign (officially called the fragment identifier) is a client-side only part of the URL. It's never send to the server. That's why it won't work for rewrite but only for redirects. The rewrite rule will match but IIS will actually try to open a file called hashtest.aspx#hash1 (i.e. a file with the extension .asp#hash1). This file won't be processed as a normal ASP page as the extension is not linked to ASP.NET. And most likely it's content won't even be displayed at all as IIS is by default configured to only allow request for known extensions.

IIS URL Rewriting At Initial Host Request

I am having a problem with an IIS7 Integrated Pipeline URL Rewrite.
For my particular scenario I need to rewrite/redirect part of the initial request as follows.
User enters http://savecontoso.com/files/123456789/somefile.html in the browser address bar.
User is redirected to http://savecontso.com/default.aspx?url= (results of url="default.aspx?url={R:1}")
This currently works as expected only if I create the initial request as such, http://savecontoso.com/default.aspx/files/123456789/somefile.html.
I must note that there is no actual directory of /files/ nor /123456789/ nor any file named somefile.html on the server. I simply need that entire path and filename appended to a query string.
This is my first day working with redirect/rewrite functions using IIS instead of page code behind and I have looked all around learn.iis.net, Google etc to no avail. I understand that rewriting takes place before page requests but for some reason my particular code requires a page request before firing the redirect.
I suspect it is because I am not triggering conditions at the initial request?
<rewrite>
<rules>
<rule name="1" stopProcessing="true">
<match url="(.*)(/files/\d+/.*html$)" />
<action type="Redirect" redirectType="Permanent" url="default.aspx?url={R:1}" />
</rule>
</rules>
</rewrite>
Most likely it does not work because of your match pattern:
the {R:1} will only match (.*) in your pattern, and will never match files/123...
URL in match pattern always starts with no leading slash: should be files/\d+... and not /files/\d+...
Try this one instead (works fine for me):
<rule name="1" stopProcessing="true">
<match url="^files/\d+/.*\.html$" />
<action type="Redirect" url="default.aspx?url={R:0}" redirectType="Permanent" />
</rule>

Resources