URL Rewriting

From AwasuWiki
Jump to: navigation, search

What it does

Using the XSLT file below, Awasu can automatically alter a feed item's URL. This can be useful to "eliminate the middle man" where you are receiving feed items from one source that is really providing content from another's web site. In some cases the "middle man" may be altering the original URL by appending a URL that points back to their site and then re-routes you to the original URL; in this way they can track your click activity.


So this:

http://news.google.com/news/url?sa=t&fd=R&usg=AFQjCNGym52tQ1Zt7pbtRoIdd0lNcZKw2Q&url=http://www.dailynexus.com/2011-10-20/crazy-san-francisco-49ers/

simply becomes this:

http://www.dailynexus.com/2011-10-20/crazy-san-francisco-49ers/

How it works

The XSLT file looks for url= in the URL, if it finds url= in the URL it returns only the remainder of the URL that appears after url=.

Customization

If needed you can change the value of <xsl:variable name="UrlPrefix">url=</xsl:variable> to process other feeds that use a different prefix before the actual URL.

Instructions

  • Save a copy of the XSL file below.

The recommended location is the XSLT directory in Awasu's installation directory.

  • Open the Channel Properties dialog for the channel you want to reverse (or start the New Channel Wizard), click on the Processing button and browse to the XSLT file you just saved. The feed items will be reversed the next time the channel is updated.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:atom="http://www.w3.org/2005/Atom">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  <xsl:variable name="UrlPrefix">url=</xsl:variable>
  <!--
  Matches everything not specified elseware; copies it to the output.
  -->
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>  
  
  <!--
  Matches the root node and "gets the party started".
  -->
  <xsl:template match="/">
    <xsl:apply-templates></xsl:apply-templates>
  </xsl:template>
  
  <!--
  Matches RSS <link> elements.
  -->
  <xsl:template match="/rss/channel/item/link">
    <xsl:element name="{name(.)}">
      <xsl:choose>
        <xsl:when test="contains(., $UrlPrefix)">
          <xsl:value-of select="substring-after(., $UrlPrefix)"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates/>
        </xsl:otherwise>
      </xsl:choose>

    </xsl:element>
  </xsl:template>

  <!--
  Matches Atom <link> elements.
  -->
  <xsl:template match="/atom:feed/atom:entry/atom:link/@href">
      <xsl:choose>
        <xsl:when test="contains(., $UrlPrefix)">
          <xsl:value-of select="substring-after(., $UrlPrefix)"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates/>
        </xsl:otherwise>
      </xsl:choose>
  </xsl:template>

</xsl:stylesheet>