Reputation: 12541
What is the best way to force lower case URLs on rewrite?
All has failed...what am I doing wrong? See the following:
Global.asax
I don't know which method to use?
void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext context = HttpContext.Current;
string path = Request.Path.ToLower();
context.RewritePath(path);
}
web.config
I have tried using the web.config method but can't manage:
<rewrite url="(.*)$" to="$1" permanent="true" />
How can I change the above code so that it automatically force the url to lower case?
Upvotes: 1
Views: 1661
Reputation: 340
Use the url rewrite tag in the web.config. If vs2012 does not recognise the tag, run the script found on this page: http://blog.vanmeeuwen-online.nl/2013/04/visual-studio-2012-xml-intellisense-for.html Here are two other links that i used to figure things out. ScottGu'sBlog and Meligy's Blog
<system.webServer>
<rewrite>
<rules>
<rule name="LowerCaseRule" stopProcessing="true">
<match url="[A-Z]" ignoreCase="false" />
<action type="Redirect" url="{ToLower:{URL}}" />
</rule>
</rules>
</rewrite>
</system.webServer>
Upvotes: 1
Reputation: 27944
There us no way you can force your user
to write lower case urls. All the urls you
generate can be lower case. If you get a uppercase in your url you can redirect your user to a lowercase version.
void Application_BeginRequest(object sender, EventArgs e)
{
string path = Request.Path;
if (ContainsUpperChars(path))
{
HttpContext.Current.Response.Redirect(Request.Path.ToLower());
}
}
bool ContainsUpperChars(string str) { some code to test for uppercase chars }
Upvotes: 1
Reputation: 301
Have you tried the URL rewrite module for IIS?
http://www.iis.net/download/urlrewrite
There is a particular rule for lower case:
Upvotes: 2