How Do I Feed Data To A PHP Script Without Question Marks In The URL?
Mar 29th, 2007 by Greg Bulmash
You've all seen the URL that looks like this... http://www.somesite.com/article.php?articleid=598&refloc=hp. You've also seen URLs that look like this... http://www.somesite.com/article/598/hp/. The interesting thing is that it only takes about 4 more lines of computer code for the shorter URL to do the exact same thing as the longer one.
Although it's not so much of a problem now, years back the search engines had issues with URLs that used question marks and ampersands in them. If you wanted better indexing of your site you'd do a lot better if your URLs looked like http://www.somesite.com/article/598/hp/ than http://www.somesite.com/article.php?articleid=598&refloc=hp.
Nowadays, converting ?articleid=598&refloc=hp to /598/hp/ serves other purposes.
- One, it makes the URL shorter and easier to e-mail.
- Two, search engines give more relevance to directory names than to variables, so the /598/hp/ structure is a step in the right direction on your SEO efforts.
- Three, some lower-quality mail programs that automatically link URLs in e-mails stop linking at the question mark or ampersand, truncating and breaking the URL, so the /598/hp/ structure makes your links more universally clickable in e-mails.
- Four, well, it just makes the URLs look better.
But how the heck do you do it? I'm going to tell you.
Part I: The .htaccess file
Doing it is broken down into two parts. Part 1 is making sure your web server knows what to do. With most servers, this means the .htaccess file. Yes, that's right, there's nothing on the left side of the dot. On servers that run Unix, Linux, or BSD, many configuration files have names that start with dots.
You may or may not have a .htaccess on your server already. If you do, you'll want to download and edit it. If not, you'll want to create one. Your text editor may balk at creating a file that doesn't end in ".txt". If it does, that's okay. You can always rename the file to .htaccess later.
Once you're editing the .htaccess file, you'll want to add these three lines...
<Files article>
SetHandler application/x-httpd-php
</Files>
Now notice we're using "article" and not "article.php". This addition to .htaccess is telling the server that "article" is a PHP script, so it doesn't need the ".php" on the end to tell it that. We do this so you can use the clean-looking /article/598/hp/ URL instead of /article.php/598/hp/.
Part II: Your PHP Script
Previously, your script may have been using $_GET or $_REQUEST to get the values of "article" and "refloc". And one of the advantages to the question mark and ampersand method is you could add as many variables you wanted in whatever order you wanted. With the cleaner method we're using here, you have to plan ahead.
The data that goes into the URL after http://www.somesite.com/article/ has to be structured so you know what comes in what order. If you have http://www.somesite.com/article/A/B/ the type of data in A has to be the same every time. It can't be the article number sometimes and the referring source sometimes. It's always going to need to be the article number or you're going to have to do some fancy footwork in your programming.
Now that you're using this method, instead of using something like $_GET["article"] to get the article value, you'll switch to the following...
$valuearray = explode("/",$_SERVER["REQUEST_URI"]);
If your URL is http://www.somesite.com/article/A/B/, the value of $_SERVER["REQUEST_URI"] will be /article/A/B/. The "explode" function will split it into an array, splitting on the slashes, so $valuearray will have the following values...
- $valuearray[0] = "";
- $valuearray[1] = "article";
- $valuearray[2] = "A";
- $valuearray[3] = "B";
- $valuearray[4] = "";
So, while the code to get values for http://www.somesite.com/article.php?articleid=598&refloc=hp would be...
$articleid = $_GET["articleid"];
$refloc = $_GET["refloc"];
...the code to get values for http://www.somesite.com/article/598/hp/ would be...
$valuearray = explode("/",$_SERVER["REQUEST_URI"]);
$articleid = $valuearray[2];
$refloc = $valuearray[3];
Of course, for better SEO, you might want to make the url /article/598/how_to_make_lemonade/hp/ instead of /article/598/hp/. But you don't have to add any code. You've put "how_to_make_lemonade" in there so the search engines see it and associate your article more strongly with making lemonade, but the script doesn't need it. As long as you always have that article title in there, then $refloc would take the value of $valuearray[4] instead of $valuearray[3], and you just adjust that one number in your code.
In some of my uses of this, the script only needs one value, so I put it at the end, and then put whatever SEO stuff I want in the URL with as many slashes as I want. While the end position might be 4th or 8th, it's always last, so I always know where to find the information I need.
If you go through my sites, though, you'll find I use this method in some places, but I still use question marks and ampersands in others. Though the slashes method is cleaner and easier to optimize for search engines, it's also more work, and the more variables you're passing in the URL, the more likely you are to accidentally get them out of sequence and get unintended results. So, though you can do this everywhere, it's best used in the places where it's most useful. Elsewhere, the old question mark and ampersand method will work just fine.
I love this article, but I do have one question. Our site is run on a Windows server and I was wondering how you can do Part 1 on a windows server or if it's even possible to do this on a windows server?
Kevin,
By "windows server", I assume you mean one running Microsoft's web server software Internet Information Server (IIS). I've run Windows Server 2003 with Apache, and Part I doesn't need to be changed in that case. But in the case of IIS, the .htaccess file doesn't work.
See this Microsoft article on migrating .htaccess to IIS. It's not 100% clear, but it's a starting point.
Thank you so much Greg, I'm sorry I should have specified IIS as my problem I was running to class when I posted the last message. I'll be checking out that article right now.
This page has been bookmarked for future reference! Great Article! Thanks for sharing!