Avoid uppercase letters in your Episerver page URL
For a number of reasons it's widely recommended to use all lowercase URL's inside a web site. Unfortunately EPiServer out of the box uses the same casing as the entered PageName (unless you modify the URL manually).
In order to always get a lowercase URL here's some code we've been using for ages. Hook it up on the SavingPage and CreatingPage events and make your web site a little bit better.
using EPiServer.Web;
..
if (string.IsNullOrEmpty(page.URLSegment))
{
page.URLSegment = UrlSegment.CreateUrlSegment(page);
}
if (!string.IsNullOrEmpty(page.URLSegment))
{
page.URLSegment = page.URLSegment.ToLower();
}
if (page.URLSegment.Contains("--"))
{
page.URLSegment = Regex.Replace(page.URLSegment, "--+", "-");
}
UPDATE: Check out Joel's blog post for a complete example compatible with EPiServer 7.
Published Wednesday 16 January 2013 18:20 and tagged with these categories: Episerver
Good post. Lowercase URLs is definitely one of those simple SEO fixes that should come out of the box. We've been using the same technique, but another approach is to place your EventHandler in Application_Start (global.aspx.cs), it will then take effect for every event where CreateUrlSegment is invoked.
http://www.epinova.no/blog/Lars-Timenes/dates/2010/4/SEO-Lower-case-URLs/
Also contains some tips on how to fix existing URLs with mixed casing :-)
Nice! I didn't know of the CreatedUrlSegment event. You should add it to epinova.no btw ;-)
Replacing "--" with "-" in CreatedUrlSegment will create conflicting urls. Imagine you have published a page with page name "Page--name" and with "Page-name" under a specific page. If you replace "--" in CreatedUrlSegment it will generate the same url for both pages.
Thanks for commenting! The chances are fairly slim I would say. That replace mostly catches double dashes being generated from strings such as "& " in a PageName. I'd rather have a conflict once in a million than a ugly URL once in ten. But I also think EPi adds a number if the segments are identical, no?
Yes, EPiServer makes the URL unique but that occurs before CreatedUrlSegment is called (Thanks to Reflector:) ).
You could ofcourse add a rewrite rule to the web.config like this:
<rule name="Force lower case" stopProcessing="true">
<match url=".[A-Z]." ignoreCase="false" />
<conditions>
<add input="{REQUEST_URI}" pattern="^/EPiServer/" negate="true" />
<add input="{URL}" pattern="^.*\.(axd|css|js|jpg|jpeg|png|gif)$" negate="true" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="{ToLower:{URL}}" />
</rule>
Hi Joroen,
Your suggestion is it works for me. but the toolbox for composer not display properly. Any idea about this please? Thanks.,