Friendly URL with no rebasing
I got a question regarding EPiServer CMS friendly url rewriting about wether it is possible to make the rewritten url's relative to the site's root rather than the context of the requested page.
If you look at the html source for a page rendered by EPiServer CMS you'll notice that all links are relative to the requested page, and if that page is some levels "down" in the hierarchy, the link to the start page could for example look like this:
<a href="../../../../../" target="_top">EPiServer Labs</a>
Although it will work, it doesnt look very nice, and could lead to potential problems if you try to reuse the html code "outside" of the website context.
So, in order to prevent this, I hacked up a small extension to the default FriendlyHtmlRewriteToExternal class shipped with EPiServer CMS which does not perform such rebasing on the converted links.
The code is very simple and contains just two classes, FriendlyUrlRewriteProviderNoRebase and FriendlyHtmlRewriteToExternalProviderNoRebase (arent those names great or what?!).
The FriendlyUrlRewriteProviderNoRebase class simply inherits from FriendlyUrlRewriteProvider and overrides the GetHtmlRewriter() to return an instance of class two, FriendlyHtmlRewriteToExternalProviderNoRebase.
1: public class FriendlyUrlRewriteProviderNoRebase : FriendlyUrlRewriteProvider
2: { 3: public override HtmlRewriteToExternal GetHtmlRewriter()
4: { 5: return new FriendlyHtmlRewriteToExternalNoRebase();
6: }
7: }
And, the class FriendlyHtmlRewriteToExternalProviderNoRebase overrides the method HtmlRewriteUrl to perform the same function as the base implementation but _without_ doing the UrlBuilder.Rebase()-call.
1: public class FriendlyHtmlRewriteToExternalNoRebase : FriendlyHtmlRewriteToExternal
2: { 3: protected override bool HtmlRewriteUrl(UrlBuilder internalUrl,
4: UrlBuilder externalUrl,
5: UrlBuilder url,
6: System.Text.Encoding encoding,
7: out object internalObject)
8: { 9: internalObject = PermanentLinkUtility.GetPageReference(url);
10: return Global.UrlRewriteProvider.ConvertToExternal(url, internalObject, encoding);
11: }
12: }
In order to use these new classes, they'd have to be configured through the web.config file under the section <episerver><urlRewrite>:
1: <urlRewrite defaultProvider="FriendlyUrlRewriteProviderNoRebase">
2: <providers>
3: <add name="FriendlyUrlRewriteProviderNoRebase" type="Dev.FriendlyUrlRewriteProviderNoRebase, Dev" />
4: <add name="EPiServerFriendlyUrlRewriteProvider" type="EPiServer.Web.FriendlyUrlRewriteProvider,EPiServer" />
5: <add description="EPiServer identity URL rewriter" name="EPiServerIdentityUrlRewriteProvider" type="EPiServer.Web.IdentityUrlRewriteProvider,EPiServer" />
6: <add description="EPiServer bypass URL rewriter" name="EPiServerNullUrlRewriteProvider" type="EPiServer.Web.NullUrlRewriteProvider,EPiServer" />
7: </providers>
8: </urlRewrite>
The complete source file can be downloaded here: http://labs.episerver.com/PageFiles/111555/SourceCode.zip
/johan
01 July 2008