Internal linking with a C# HttpModule


I’m still not sure all this relative vs absolute path internal linking stuff I’m reading about in SEO ins’t due to the fact there’s confusion between what an absolute link is vs a link with a fully qualified domain but here’s a http module that will include the fully qualified domain with protocol for all of your internal links.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text;

namespace MvcApplication1
{
     public class AbsolutePathRewriter : IHttpModule
    {
        HttpApplication application;
        #region IHttpModule Members
        public void Dispose()
        {
            //Empty
        }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(OnBeginRequest);
            application = context;
        }

        #endregion
        void OnBeginRequest(object sender, EventArgs e)
        {
            if (application.Request.AppRelativeCurrentExecutionFilePath.Contains(".aspx"))
            {
                application.Response.Filter = new AbsolutePathRewriterStream(application.Response.Filter);
            }
        }
    }

    public class AbsolutePathRewriterStream : MemoryStream
    {
        private Stream outputStream = null;

        public AbsolutePathRewriterStream(Stream output)
        {
            outputStream = output;
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            string bufferContent = UTF8Encoding.UTF8.GetString(buffer);
            string absolutePath = HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority;
            bufferContent = bufferContent.Replace("href=\"/", "href=\"" + absolutePath + "/");
            bufferContent = bufferContent.Replace("href='/", "href='/" + absolutePath + "/");
            outputStream.Write(UTF8Encoding.UTF8.GetBytes(bufferContent), offset, UTF8Encoding.UTF8.GetByteCount(bufferContent));
            base.Write(buffer, offset, count);
        }
    }
}

Advertisements

About Kevin Buckley
.Net web developer with a lot of experience in CMS. Currently working at Sitecore as Solutions Engineer.

2 Responses to Internal linking with a C# HttpModule

  1. Andrew Hare says:

    This is pretty cool. I had an idea: what if instead of this:

    if (application.Request.AppRelativeCurrentExecutionFilePath.Contains(“.aspx”))

    You do this instead:

    if (application.Request.ContentType == “text/html”)

    This way you could fix any request for any content with the MIME type of “text/html”. This way you wouldn’t be tied to the “aspx” extension.

    All that being said, however, I really like this solution using a module – it is clean and non-intrusive to the app.
    if (request.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: