IIS URL rewrite module url's to lowercase - asp.net

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)

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.

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.

Url Rewrite Regex to Match Some Classic ASP Files (But not all)

I have a bunch of old .asp files that I'm trying to redirect. I do not want to redirect all .asp files, just ones related to the "News" section.
Examples:
news_1-4-2009.asp
news_2-15-2008.asp
news.asp
I created a redirect rule that almost works:
<rule name="NewsRedirect" stopProcessing="true">
<match url=".*news(.*asp)?" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" appendQueryString="false" url="http://www.example.com/about/newsroom" redirectType="Permanent" />
</rule>
The problem (as you might see) is that the action url is also a match and creates an infinite loop.
Question
I'm trying to say...
Any file that starts with "news" and ends in ".asp".
Thanks for any help!
Any file that starts with "news" and ends in ".asp".
Give this a try:
^news.*\.asp$

IIS Rewrite not working (but redirection does)

I was trying to play with URL re-writing using the Rewrite Module 2.0 but I had no luck getting it to work. What I'm trying to do is re-write all calls to web app at port 80 to other applications hosted in IIS (or maybe on different servers on the network). Using the GUI provided by IIS I created the following rule:
<rewrite>
<rules>
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="site1/(.*)" />
<action type="Rewrite" url="http://localhost:7001/{R:1}" />
</rule>
</rules>
</rewrite>
Quiet simple, but unfortunately it does not work. On the other hand, when I change the action type to Redirect, it works fine.
What could be the problem?
I ran into this same issue yesterday, and it took me a long time to figure out.
The key here is that you've got an http:// prefix in your rewrite action; that makes this a special case that needs to be handled by Application Request Routing. The first step is to make sure that the Application Request Routing module is installed. You can find the module at https://www.iis.net/downloads/microsoft/application-request-routing. Once that is installed, go to your IIS web server (a level up from your web site), and open the Application Request Routing Cache feature. From the actions on the right, choose Server.Proxy.Settings, and make sure that the "Enable Proxy" checkbox is checked. This allows the URL rewrite task to be re-routed to Application Request Routing, and your reverse proxy should work for external requests.
The idea came from this excellent blog post from 2009: http://ruslany.net/2009/04/10-url-rewriting-tips-and-tricks/
Stumbled across this old post when I was trying to solve the same issue.
SOLVED!
Using Rewrite URL feature in IIS Services Manager I created a friendly URL rule.
This worked ok and when I looked at the rule in the web.config file (www root) it showed 1 rule to redirect and 1 rule to rewrite.
I edited this to suit 1 match. Then I just duplicated this code editing the product ID for each. Example below:
<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
<match url="^product\.php$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^id_product=\b35\b" />
</conditions>
<action type="Redirect" url="990mm-bohemia-cast-iron-electric-radiator"
appendQueryString="false" />
</rule>
The first rule looks for the string "product.php" in the URL and "id_product=35", it then redirects to "990mm-bohemia-cast-iron-electric-radiator" which currently does not exist. Then (see below)
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="^\b990mm-bohemia-cast-iron-electric-radiator\b" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="product.php?id_product=35" />
</rule>
This rule rewrites the "product.php?id_product=35" bit to `990mm-bohemia-cast-iron-electric-radiator", creating the new location for the redirect.
Do make sure MVC routing doesn't steal your request. To prevent that from happening, ignore the route you're trying to rewrite:
RouteTable.Routes.Ignore("blog/{*pathInfo}");
Inspired by: https://sitecore.stackexchange.com/questions/3645/how-to-setup-a-reverse-proxy-with-sitecore
Change the Rewrite URL to AbsolutePath instead putting http://...
it should be
<action type="Rewrite" url="{R:1}" />
It worked for me, but in my case, I have been rewrite to a fixed webpage.

ASP.NET / IIS7 Url Rewrite maps not working

I've followed the instructions Learn IIS's webpage for adding static redirects with a rewrite map for my asp.net application.
The following is the config:
<rule name="Redirect rule1 for Information" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{Information:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}" appendQueryString="true" />
</rule>
And
<rewriteMaps>
<rewriteMap name="Information">
<add key="/Information/CorporateSales.aspx"
value="/KB/Information/CorporateSales" />
<add key="/Information/ComputerRepair.aspx"
value="/KB/Information/ComputerRepair" />
</rewriteMap>
</rewriteMaps>
This was even originally created by the wizard in IIS's manager for using rewrite maps.
So the idea is that /Information/CorporateSales.aspx --> /KB/Information/CorporateSales with a 301 redirect (MOVED PERMANENTLY).
However I'm just getting the original aspx page (Which we're removing later) loading. I've even deleted the file incase it was defaulting to an existing resource, and with that i just get a plain 404 without the redirect.
Anyone have an idea?
Let me clarify something:
Rewrite module works, it's installed and running. My standard regex rules work nicely. But my rewrite map does not.
This article http://www.iis.net/learn/extensions/url-rewrite-module/using-rewrite-maps-in-url-rewrite-module and code below worked for me.
<rewrite>
<rules>
<rule name="Redirect rule1 for RedirectURLs">
<match url=".*" />
<conditions>
<add input="{RedirectURLs:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="RedirectURLs">
<add key="/privacy.php" value="/privacy" />
</rewriteMap>
</rewriteMaps>
</rewrite>
I was having a similar problem and found this question. It took me a little while, but I was able to figure out what the problem was.
My rewriteMap contained the urls "/Default2.aspx" and "/Dashboard.aspx".
When I would go to Default2.aspx, I would get a 404 rather than get redirected to Dashboard.aspx as expected.
The issue I found was that on my machine, the application was running in a subdirectory. The rewriteMap paths would only work if I used the full path (including the application folder), e.g., "/TestSite/Default2.aspx".
So I could have added duplicate entries in my rewriteMap to account for application directories on developer machines, but that seemed messy. I looked at the other rewrite rules in the application that did not have this issue and I noticed that they were using the {REQUEST_FILENAME} variable, rather than {REQUEST_URI}. So I switched the rule to use {REQUEST_FILENAME} and remove the first slash from the urls in my rewriteMap.
Do you have Url rewriting installed as part of IIS7/7.5? This is not installed by default. Also, make sure your app pool is set to integrated pipline mode, no classic.
Edit
From this:
http://learn.iis.net/page.aspx/469/using-rewrite-maps-in-url-rewrite-module/
This only thing I see that you're doing is adding the 'stopProcessing' attribute. Have you tried removing that?
Previously I had same problem as you described.
Could you update your code to
<match url="(.*)" />
and I hope you aware,
<add input="{Information:{REQUEST_URI}}" pattern="(.+)" />
this condition will capture full URL except the domain.
example on this url:
www.example.com/Information/CorporateSales.aspx
it will check matching condition of
Information/CorporateSales.aspx on rewriteMap
and for sure it wont be match with this url
www.example.com/old/Information/CorporateSales.aspx
Did you reset the app pool and the iis site ?
In some cases it can take up to 72 hours (iirc) to propagate throughout the world.

Resources