Easy SEF URL’s with PHP
Just a little brainstorm on how to make your links SEF.
- Old: content.php?id=58&page=3
- New: content/Some-crazy-title/58/3
Basically, what I like to do is have the title of the page I’m showing (like an article title) as the first part of the URL, and then the “important” information like ID’s and page numbers etc. go after it. As long as you follow the same “layout” of the URLs in every URL you make, then you can use the information as if it were a query string. So if you use Title/ID/PageNum, then use it everywhere!
The first thing you need to do is to create the .htaccess file that will make the system parse extensionless files as PHP. What does this do? Well instead of an ugly content.php/Some-crazy-title/58/3, we have content/Some-crazy-title/58/3. The former is a cheap trick if you ask me, I’m not sure if the SE’s care. But with the latter, ‘content’ looks like it’s a directory of it’s own and not simply a PHP script.
First thing you need to do is create an extensionless file. In our example, we’ll name it “content”. This is the code I’ll put in my .htaccess to forcetype PHP on that file:
[code]
ForceType application/x-httpd-php
The bolded part you will replace with the name of your file (you can also add more then one file to this directive. Just separate them by spaces).
Now what I like to do is create an array of the order of my variables. For example, in the above example we have Title/ID/Page. So I’d create an array like this:
[code]$get_order = array('title', 'id', 'page');[/code]
Then we want to get the string after ‘content/’ so we can see which ‘variables’ were passed with the URL. To do this we just use strpos() and substr() to just get the string from content/ to the end of $_SERVER['PHP_SLEF'].
[code]$url_vars = substr($_SERVER['PHP_SELF'], strpos($_SERVER['PHP_SELF'], 'content/') + strlen('content/'));[/code]
strlen() onto the position of strpos()? Because strpos() would return the position of content/, but it would be the position of the ‘c’. We need the position of the last character, ‘/’.[/code]
Now we can use explode() to explode the string into bits where we can use them in accordance the the order we set with the $get_order array. I like to stick the variables back into the $_GET array so virtually any script can be hacked to utilize these SEF's (though timely to replace all the URL's in the templates and whatnot).
[code lang="php"]
$url_vars = explode('/', $url_vars);
foreach($url_vars as $k => $v)
{
$index = $get_order[$k];
$_GET[$index] = $v;
}
?>[/code]
So now we could use $_GET['title'] or $_GET['id'] as if we were using the traditional means of query strings. Here's the full snippet of code:
[code lang="php"]
$get_order = array('title', 'id', 'page');
$url_vars = substr($_SEVRER['PHP_SELF'], strpos($_SEVRER['PHP_SELF'], 'content/') + strlen('content/'));
$url_vars = explode('/', $url_vars);
foreach($url_vars as $k => $v)
{
$index = $get_order[$k];
$_GET[$index] = $v;
}
?>[/code]
Here's a simple example of using an 'ID':
[code lang="php"]
$get_order = array('title', 'id');
$url_vars = substr($_SERVER['PHP_SELF'], strpos($_SERVER['PHP_SELF'], 'content/') + strlen('content/'));
$url_vars = explode('/', $url_vars);
foreach($url_vars as $k => $v)
{
$index = $get_order[$k];
$_GET[$index] = $v;
}
//-----------------------------------------------
if(!isset($_GET['id']))
die('No ID!');
switch($_GET['id'])
{
case 1:
echo 'Cool';
break;
case 2:
echo 'Cool cool';
break;
case 3:
echo 'Cool cool cool';
break;
default:
echo 'Not cool';
break;
}
?>[/code]
http://localhost/sef/content/testing/2 produces 'Cool cool'.
Cool eh?
You’re using WordPress… why don’t you have this enabled here? ;)
What? It’s not a feature of WP is it?
Ah, it is. But causing some problems with my design. I took out the hardcoded URL’s and replaced them with relative paths…
There, enabled it ^^