Using a server rewrite, a url such as http://example.org/jesse could read from http://example.org/jesse.php. However, in the content management system the link would go to the .php version. How can the URL be rewritten from:

<a href="http://example.org/jesse.php">Jesse</a>

to:

<a href="http://example.org/jesse">Jesse</a>

Another use case might be that /index.html should be replaced with /. This variation is explained towards the bottom.

Step 1: Isolate the anchor

In any software development the best way to tackle a problem is to break it up into smaller problems. Try something and if it works, expand the problem. The first step in solving this issue would be to do a template match for every anchor that contains .php and rewrite the anchor.

  • Line 1 – match any anchor tag where the href contains .php.
  • Line 2 – writes an anchor tag with the attribute href with a value that replaces ‘.php’ with ”.
  • Line 3 – write the text of the link.

This works for the basic link above because it doesn’t have any other attributes. But if the anchor had a class or id, those would be stripped. For example, this:

<a href="http://example.org/jesse.php" class="button">Jesse</a>

would render

<a href="http://example.org/jesse">Jesse</a>

Not cool.

Step 2: Loop through all the attributes and when href is matched, replace the .php

  • Line 1 – Same as line 1 from the previous example.
  • Line 2 – Create the html element <a>
  • Line 3 – Loop through all of the attributes. In XSL attributes are represented as @.
  • Line 4 – xsl:choose statement
  • Line 5 – When the attribute name equals ‘href’ then write the attribute with a modified URL using lines 6 & 7
    • Line 6 – Create an ‘href’ attribute.
    • Line 7 – Write the URL and replace ‘.php’ with ”. Similar to line 2 in the previous example.
  • Line 10 – Otherwise, the attribute name is not ‘href’ then write the attribute using lines 11 & 12
    • Line 11 – Create an attribute with its original name. This could be class, id, etc.
    • Line 12 – Write the value of the attribute.
  • Line 18 – Write the name of the text link. Similar to line 3 in the previous example.

Step 3: Expand to other extensions or to index.*

.aspx/.html/.htm/.whatever

Change line 1: <xsl:template match="a[contains(@href, '.aspx')]">

Change line 7: <xsl:value-of select ="replace(., '.aspx, '')"/>

index.html

This approach would also work for removing /index.html in URLs. This helps with SEO and makes analytics cleaner*.

Change line 1: <xsl:template match="a[contains(@href, 'index.html')]">

Change line 7: <xsl:value-of select ="replace(., 'index.html', '')"/>

  • Another solution for analytics would be to create a filter to change /index.* to /.

This post is an expansion of a forum response that I gave on the OUCampus private forum. The problem was fun to work through so I decided to create a blog post from it.