Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

rawurlencode and ampersand's problem

Status
Not open for further replies.

Jeff80

Programmer
Aug 18, 2005
9
0
0
US
I'm trying to include a title in the url of a link that contains an ampersand. For example, the link would look like:

& blah/blah.html

I'm using rawurlencode on the "blah & blah" part to make the link safe, but when I click on the link, it tries to load the page:


It is cutting off everything after the ampersand. What's the best way to translate the ampersand so that it works?

Thanks,
-Jeff
 
First of all, it is not a great idea to have a URL that uses an ampersand for any other functionality than the querystring. Spaces in URLs also are not the greatest idea.

I would run htmlentities() on the URL first, then the urlencode.

Anyway, please post your code so we can see the encoding and decoding.

 
I knew ampersands and spaces were not good in the url if left untranslated. I am creating a library of articles with the articles divided by category. Let's say one of the categories is called "Health & Fitness". I wanted to put it directly in the link for the articles to improve search engine ranking. For example, the link might look like:

& Fitness/article1.html

The "Health & Fitness" isn't an actual directory on the server. It's just being used with mod_rewrite so my dynamic links look static - again for SEO.

I knew the name "Health & Fitness" couldn't be put in directly. I was using:

<a href=" & Fitness")/article1.html">Article 1</a>

Using that function, I would always get "Health%20%26%20Fitness" in the url which I thought would work. However, when I click on it, it tries to load the link:


As a temporary fix, I'm just using a custom function I wrote to replace commonly-used special characters like the ampersand (eg: & becomes "and").
 
The URL encoding will be done by the client. You need not worry anout that. You can replace the ampersand with the HTML entity. But I think urlencoding is not necessary.

Have you tried the straight URL?

N.B>
Most search engines do well nowadays with the URL containing querystrings.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top