Page 1 of 1

Share tips for keeping URL length short!

Posted: Sat Aug 15, 2009 12:49 pm
by JAB Creations
My screen's native resolution is 1920x1200 and even though the address bar is 50% of that width I still cringe when I see obscenely long URL's. So I thought I'd start a short URL tips thread so we can learn each other's tactics for keeping URL's reasonably short!

This morning I've been working on the member index page that simply shows a list of registered members on my site. Locally I have error_reporting set to E_ALL as I like forcing myself to code to a higher standard whenever possible. One of stricter warnings with this error mode are undefined variables (e.g. $_GET['something'] is not defined). I was trying to figure out how to get the order URL's to work without generating any PHP warnings and then I realized instead of having the URL look something like members/?column=alias&sort=desc I could simply use members/?alias&desc. This meant instead of first determining if the value was set and then what it was set to...and still having to repeat the same steps (since there are multiple columns to sort by) all I had to do was execute a simple isset to determine if I should echo anything special out.

Here is some example code not yet ready for a live environment though it should give an idea of how simple this approach is. Suggestions for improvement are of course welcomed!

Code: Select all

echo '<thead>'."\n";
echo '<tr><td><a class="block" href="members/';
if (!isset($_GET['alias'])) {echo '?alias&desc';}
echo '" rel="nofollow" tabindex="3" title="">Alias</a></td><td><span>Location</span></td><td><a class="block" href="members/';
if (isset($_GET['posts'])) {if (!isset($_GET['desc'])) {echo '?posts&desc';} else {echo '?posts';}} else {echo '?posts';}
echo '" rel="nofollow" tabindex="3" title="">Posts</a></td><td><a class="block" href="members/';
if (isset($_GET['rank'])) {if (!isset($_GET['desc'])) {echo '?rank&desc';} else {echo '?rank';}} else {echo '?rank';}
echo '" rel="nofollow" tabindex="3" title="">Rank</a></td><td><a class="block" href="members/';
if (isset($_GET['registered'])) {if (!isset($_GET['desc'])) {echo '?registered&desc';} else {echo '?registered';}} else {echo '?registered';}
echo '" rel="nofollow" tabindex="3" title="">Registered</a></td></tr>'."\n";
echo '</thead>'."\n";
I'm curious about what others have done to keep their URL length reasonable?

Re: Share tips for keeping URL length short!

Posted: Wed Aug 19, 2009 12:07 pm
by AlanG
I don't really have any suggestions regarding the url length as I don't consider it an issue.
I do have a suggestion for your validation however.

Code: Select all

<?php if (!isset($_GET['alias'])) {echo '?alias&desc';} ?>
If ?alias= is all thats entered into the url, the above condition will return true. You should check if the variable is empty also

Code: Select all

<?php if (!isset($_GET['alias']) && !empty($_GET['alias'])) {echo '?alias&desc';} ?>

Re: Share tips for keeping URL length short!

Posted: Wed Aug 19, 2009 12:25 pm
by matthijs
Or use clean urls when possible ('/members/jab/'). Or else if you need the query, you can of course use shorter items ('?a=10&b=d')