Choy
Choy

Reputation: 2117

URL encoded links in RSS breaks links

In order to have a valid XML RSS feed, I need to url encode my links which contain '&' in the query string. However, when a person views the RSS feed in a browser, the links are not decoded, so when they click on the link the resulting page is broken because the query string parameters are not being read correctly.

E.g.

http://www.mysite.com?foo1=bar1&foo2=bar2, when viewed in the browser as http://www.mysite.com?foo1=bar1%26foo2=bar2, the parameter foo1 is being taken as bar1%26foo2=bar2 instead of splitting fruit and drink into two separate parameters.

Wrapping my links in tags and not encoding them work, but then in my MRSS feed I have fields such as where including a tag breaks the XML.

How can I go about correctly encoding my links so that everything works?

Upvotes: 5

Views: 3234

Answers (1)

Conrad Frix
Conrad Frix

Reputation: 52675

Short answer

use &amp

Long answer

If you look at https://stackoverflow.com/feeds/tag/xml, the entry for this question looks like so and your sample link works fine when viewed in I.E.

Your sample link

http://www.mysite.com?foo1=bar1&foo2=bar2

Your entry

<entry>
        <id>https://stackoverflow.com/questions/5705246/url-encoded-links-in-rss-breaks-links</id>
        <re:rank scheme="http://stackoverflow.com">0</re:rank>
        <title type="text">URL encoded links in RSS breaks links</title>
        <category scheme="https://stackoverflow.com/feeds/tag/xml/tags" term="xml"/><category scheme="https://stackoverflow.com/feeds/tag/xml/tags" term="rss"/><category scheme="https://stackoverflow.com/feeds/tag/xml/tags" term="urlencode"/>
        <author>
            <name>Choy</name>
            <uri>https://stackoverflow.com/users/252529</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/5705246/url-encoded-links-in-rss-breaks-links" />
        <published>2011-04-18T15:30:11Z</published>
        <updated>2011-04-18T15:35:43Z</updated>
        <summary type="html">
            &lt;p&gt;In order to have a valid XML RSS feed, I need to url encode my links which contain &#39;&amp;amp;&#39; in the query string.  However, when a person views the RSS feed in a browser, the links are not decoded, so when they click on the link the resulting page is broken because the query string parameters are not being read correctly.&lt;/p&gt;

&lt;p&gt;E.g.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://www.mysite.com?foo1=bar1&amp;amp;foo2=bar2&quot; rel=&quot;nofollow&quot;&gt;http://www.mysite.com?foo1=bar1&amp;amp;foo2=bar2&lt;/a&gt;, when viewed in the browser as &lt;a href=&quot;http://www.mysite.com?foo1=bar1%26foo2=bar2&quot; rel=&quot;nofollow&quot;&gt;http://www.mysite.com?foo1=bar1%26foo2=bar2&lt;/a&gt;, the parameter foo1 is being taken as bar1%26foo2=bar2 instead of splitting fruit and drink into two separate parameters.&lt;/p&gt;

&lt;p&gt;Wrapping my links in  tags and not encoding them work, but then in my MRSS feed I have fields such as  where including a  tag breaks the XML.&lt;/p&gt;

&lt;p&gt;How can I go about correctly encoding my links so that everything works?&lt;/p&gt;

        </summary>
    </entry>

Upvotes: 4

Related Questions