Avoid Broken Website Links by Eliminating Whitespace in GET Variables

When naming pages for a website, its best practice to avoid spaces. Well the same goes for the GET variables (and their values) passed via the URL. Website addresses will likely work with or without spaces. However, that doesn't mean they will be free from problems. Visitors may copy a website address into an e-mail message, for example, and those spaces prevent e-mail clients like Microsoft Outlook from converting the address into a clickable link. At least a clickable link that works.


I've been diligent about keeping spaces out of website file and folder names. The same thing goes for the GET variables themselves. But, for some reason, I didn't consider the effects of having spaces within the GET variable values while developing a website several years ago.

This particular website depends on GET variables since most of its content is pulled from a database. One variable, of course, had a value containing a space. As links were being shared more via e-mail, I started noticing the broken links. The entire link was pasted into the e-mail, but only a portion was clickable. Everything after the space wasn't included with the link.

The issue could have been fixed by removing the space, but I wasn't keen on breaking anyone's bookmarks or causing problems with indexes maintained by search engines like Google. Note that about 80% of the website depends on this particular GET variable. Luckily, there is another solution.


Instead of removing the spaces, we can encode them with PHP's urlencode() function.

$section         = 'Web Development';
$section_encoded = urlencode($section);

Note that the encoded value was saved to a separate variable since the unencoded value is still needed for other aspects of the page. For links, the encoded value is used.

<a href="mypage.php?section=<?php print $section_encoded; ?>">Test Link</a>

When clicking the link (or viewing the page's source code), the value for the GET variable should have a plus sign in place of the space.


Although it's probably better to pass GET variable information without the spaces, it may not always work that way. If spaces unavoidable, try the urlencode() function.

Note: there doesn't seem to be a need to decode the GET variable values once they're passed through the URL. If you notice any issues, you can use the urldecode() function.


There are currently no comments.

Leave a Comment