Using IIS7's Rewrite Module and a database - iis-7

My company converted from an old website to a new one and we have a bunch of old pages with URLs like this:
www.example.com?foo.aspx
www.example.com?foo.aspx?ID=B&utm_source=Foo
www.example.com?foo.aspx?ID=C&utm_source=Foo
Those URLs need to go to these pages respectively:
www.example.com/ProductA
www.example.com/ProductB?utm_source=Foo
www.example.com/ProductC?utm_source=Foo
I can get this to work by using in my web.config but there are so many I would prefer to do it in the database. I have been able to partially successfully switch to the database using the article http://learn.iis.net/page.aspx/803/using-custom-rewrite-providers-with-url-rewrite-module/.
My issue is that all of my initially examples redirect to www.example.com/ProductA. It is as if they are ignoring the Query Strings. Any idea how to fix this? My rule in my config file is:
<rule name="DbProviderTest" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{DB:{R:1}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" />
</rule>

the URL that is matched in the tag does not include the Query String and that is why you will not see it in your R:1, you should be able to change your condition to be something like:
<add input="{DB:{R:1}?{QUERY_STRING}}" pattern="(.+)" />

Related

Using IIS Rewrite URL for SEO to move all Cold Fusion CFM pages to ASP aspx pages

I am moving a large database driven website from coldfusion .cfm to .net aspx. I am pretty much finished now, but I need to do 301 redirects on all the coldfusion pages to the new aspx ones, so google and such like as we don't want to lose the search engine positioning. So I intended to use URL Rewrite for this, but I cannot get it working, most of the time I just get 404s back.
Basically, my new aspx pages are all the same filename, just .cfm is replaced with .aspx, some pages can have a long querystring after them and some not.
Examples:
http://www.example.com/test.cfm needs to be remapped to http://www.example.com/test.aspx
http://www.example.com/test2.cfm?a=1&b=2&c=3 needs to be remapped to http://www.example.com/test2.aspx?a=1&b=2&c=3
The site itself has hundreds of pages and some pages have over 8 variables in the querystring, so I just wanted to try and do a straight map over in URL rewrite. I cannot do a rule per page as that will take ages!
My current attempt is:
<rule name="redirect all requests" stopProcessing="true">
<match url="^(.*)$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{URL}" pattern="^http://www.example.com/(.*).cfm(.*)$" />
</conditions>
<action type="Redirect" url="http://www.example.com/{C:1}.aspx" appendQueryString="true" logRewrittenUrl="true" />
</rule>
This just does a 404 for me. Obviously the cfm pages do not exist and the aspx ones now do. I still have cold fusion installed on the server for now, and do not want to uninstall it until google has updated itself (it case of issues so I can always go back).
Any help would be much appreciated.
Thanks
David
It's like this...
<rule name="CFM301ASP" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{REQUEST_FILENAME}" pattern="^.*\.cfm$" negate="false" ignoreCase="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
</conditions>
<action type="Redirect" url="/yourfile.aspx" appendQueryString="true" redirectType="Permanent" />
</rule>
This only works on CFMs that do not exist (isFile=false). It will 301 redirect CFMs that would cause a 404, to your ASP file, and append the URL variables.
The documentation seems to cover all of this.
From the docs on HTTP Redirect:
<configuration>
<system.webServer>
<httpRedirect enabled="true" exactDestination="true" httpResponseStatus="Found">
<add wildcard="*.php" destination="/default.htm" />
</httpRedirect>
</system.webServer>
</configuration>
This led me to believe that you should be able to map URLs from one file extenstion to another.
Creating Rewrite Rules for the URL Rewrite Module
<rewrite>
<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>
</rewrite>
Try reading through these docs, you should be able to find the correct syntax that will allow you to just transfer from *.cfm to *.aspx, passing along the same query strings, or making translations as needed.

Ensure www using webconfig rewrite

I'm working with an Umbraco asp.net website and I would like to ensure website url is alwayes displayed with www I have added:
<rule name="WWW rule" stopProcessing="false">
<match url=".*" />
<conditions>
<add input="{CACHE_URL}" pattern="^(.+)://(?!www)(.*)" />
</conditions>
<action type="Redirect" url="{C:1}://www.{C:2}" redirectType="Permanent" />
</rule>
To me this is a good methode for making sure only one URL on website. Only issue as I see is that when typing DOMAINE only my default.aspx will be added to frontpage.
How to avoid that?
I have tried to add:
<rule name="Default Document" stopProcessing="true">
<match url="(.*)default.aspx" />
<action type="Redirect" url="{R:1}" redirectType="Permanent" />
</rule>
This makes frontpage inside a loop....
Have I misunderstood somthing? Thanks
Shouldn't stopProcessing="true" for the domain rewrite? I'm guessing you don't want the rest of the rules evaluated?
Also, I'm guessing you might want to use {HTTP_HOST} in you condition, since I would guess that the {CACHE_URL} would be the one including the default.aspx path?
Currently you are using the incoming http/https schema, which are also considered different url's, so you may want to choose which one to use, or implement a canonical meta tag on your site to tell search engines which schema to use.

IIS URL Rewriting and MVC

My hosting plan has a limited number of web applications for use, but unlimited subdomains. I plan to take advantage of these subdomains by using IIS rewriting, like the following:
<rule name="Home Rewrite" enabled="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^home\.mydomain\.com$" />
</conditions>
<action type="Rewrite" url="home/{R:1}" />
</rule>
This works fine for the most part, I can go to http://home.mydomain.com and it'll take me to what is essentially http://www.mydomain.com/home as expected.
I am publishing using Web Deploy, and I believe the host is IIS7.
The problem is that I want to take advantage of #Html.ActionLink, but when viewing the source, this resolves out to include the virtual directory.
So what I end up with is a site that works when I go to the original address:
http://www.mydomain.com/home/application
And a site that loads, but doesn't function correctly, at the redirected address:
http://home.mydomain.com/application
With generated URLs in the page source pointing relative to the original address:
/home/application/Account/Login
This applies to links to other pages/routes, bundles, basically anywhere that ~/ or #Html.ActionLink is used.
How do I get around this? I'm hoping to keep the use of #Html.ActionLink at least, I think I can live without the tildes.
I finally found a solution!
https://support.gearhost.com/entries/23689272-URL-Rewrite-Subdomain
My web.config rewrite rule required an extra line:
<rule name="Home Rewrite" enabled="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^home\.mydomain\.com$" />
<add input="{PATH_INFO}" pattern="^/home/" negate="true" /> <!-- This one! -->
</conditions>
<action type="Rewrite" url="home/{R:1}" />
</rule>
Now everyone is happy :)
You could write a custom HtmlHelper so instead of using ActionLink you can use MyActionLink and it can generate the url you need.

IIS URL rewrite module url's to lowercase

For better SEO we are using URL rewrite to convert all the URL's to lowercase. I set this one as mentioned in this the below article.
Everything is working fine from URL perspective, but we see lot of 301 redirects when we check in fiddler.
It looks like the images, javascript, css, jquery ajax calls and everything is getting converted into lower case.
I am trying to remove that and want to rewrite only aspx extension and no extension urls. I tried to play around the matchurl without any success. Any help or guidelines will be highly appricated.
Thanks
Edit:
My Current rule is
<rules>
<rulename="LowerCaseRule1"patternSyntax="ExactMatch"stopProcessing="true">
<matchurl="[A-Z]"ignoreCase="false"/>
<actiontype="Redirect"url="{ToLower:{URL}}"/>
</rule>
</rules>
You could probably use something as follow:
<rule name="LowerCaseRule1" stopProcessing="true">
<match url="[A-Z]" ignoreCase="false" />
<action type="Redirect" url="{ToLower:{URL}}" />
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_FILENAME}" pattern="\.aspx$" />
<add input="{REQUEST_FILENAME}" pattern="\." negate="true" />
</conditions>
</rule>
The rule will be triggered only if one of the condition is true:
The first one checks if the requested path (filename) ends with .aspx.
The second one checks if the if the requested path (filename) doesn't contain a . (so doesn't have an extension)

Stopping hotlinking from a directory using web.config

I have a folder on my site for caching large flash movies and I want to stop other people from embedding them in their site; I would like to try and do this using the web.config file only. How could this be done?
My first attempt at a rule (which doesn't work):
The following rule was supposed to prevent public access (and embedding) to .swf files in the cache folder 'CurrentCache' - http://myurl.com/ContentCache/ and give a replacement movie 'NoEmbedFromCacheSWF.swf' instead.
<rule name="Prevent SWF hotlinking" enabled="true">
<match url="^(ContentCache)(.swf)$" ignoreCase="true" />
<conditions>
<add input="{HTTP_REFERER}" pattern="^http://(.*\.)?myurl\.com/.*$" negate="true" />
</conditions>
<action type="Rewrite" url="/Content/Flash/NoEmbedFromCacheSWF.swf" />
</rule>
Thanks in advance!
Note: I think I have got the regex wrong in the <match url="A swf inside /ContentCache/" ignoreCase="true" /> line, any ideas what it should?
You can build an HttpModule for this. There is a blog posting describing exactly what you want to do I think:
HttpModule to block external referrers in ASP.NET
Edit: Of course I'm bending the rules here about web.config only. You have to use an external module, but then you can use it referencing from web.config only without modifying any of your code.
Edit2: If you want to do it using a rewrite rule, you have to change your pattern, like this:
<rule name="Prevent SWF hotlinking" enabled="true">
<match url="/ContentCache/.*\.swf$" ignoreCase="true" />
<conditions>
<add input="{HTTP_REFERER}" pattern="^http://(.*\.)?myurl\.com/.*$" negate="true" />
</conditions>
<action type="Rewrite" url="/Content/Flash/NoEmbedFromCacheSWF.swf" />
</rule>
The pattern used is a regular expression, you can read up on them here and you can test them for example on this webpage.

Resources