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:
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>
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.*
Change line 1:
<xsl:template match="a[contains(@href, '.aspx')]">
Change line 7:
<xsl:value-of select ="replace(., '.aspx, '')"/>
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
References & Process
Because no one goes alone, here are a few of the references I used and what I googled to solve the problem. I hope by describing the process it will help communicate how I think about and solve problems.
Google Search: ‘template match contains’
I was looking for the [contains()] notation.
Google Search: ‘xsl replace’
I always forget which attribute comes first in replace(). It seems like XSL, PHP and .NET all handle the order differently! I usually have to look this one up or look at past code examples that I have written.
Google Search: ‘xsl loop through attributes’
I have used xsl:loop, but only with nodes, not with attributes. At this point of solving the problem, I was unsure if looping attributes was possible, but I figured there should be a way.
Google Search: ‘xsl get node name in for-each’
Similar to above, I wasn’t sure what I would find. I had a good idea that I could get the attribute name, but I wasn’t 100% sure.
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. Comments are welcomed.