For quite a while I experimented with various .htaccess
system to give me PHP both neat and, more importantly, SEO friendly URLs. Some systems have worked better than other, but not until recently did I find one that I really like. It’s incredibly straight forward and gives you amazing flexibility. I won’t be keeping it to myself though, heres what I feel is the perfect .htaccesss
rewrite system.
The .htaccess
file
These things have always in a bane for me until this method. There difficult to debug and incredibly sensitive to even the smallest of mistakes. Luckily this method is easily copy-paste-able and very clear:
1 2 3 4 5 6 |
Options +FollowSymLinks RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-l RewriteRule ^(.+)$ index.php?url=$1 [QSA,L] |
Line by line here’s what it does. The first two lines get everything started, making sure FollowSymLinks is enabled and turning the rewrite engine on. The next three lines make sure it’s not a directory, file, or symlink each respectivly. The final line does that actual work, if it turns out it’s not a directory, file, or symlink then it redirects you back to index.php with the entire URL passed as a variable. Like I said it’s nice and simple, now it’s time to do the fun stuff…
The PHP Code
So were sending the entire path to us in PHP, thats great, and we could stop right there if you wanted to. But if you want to get a little complex, maybe pass some variables through your pseudo URL then your going to need just a touch of PHP to do it.
1 2 3 4 5 |
$_urlvars = explode('/',$_REQUEST['url']); $_urlvars['_'.$k] = $v; foreach($_urlvars as $k => $v){ $_urlvars[$v] = (isset($_urlvars[$k+1])?$_urlvars[$k+1]:''); } |
Thats all that we need in php. Breaking it down, the first line grabs the URL we passed back to it with the .htaccess
and uses explode()
to break it into chunks. After this step you have an array with each section by itself, 0 through however many parts there are. Thats great if you know where everything is going to be, and some times (most times) that would be all you need to do to have a very usable system. But for when things get fancy we have the other 4 lines.
Were going to skip the second line and start with the bottom 3, those lines look through the URL vars and add to out original standard array some associative values. This way if you have a url thats set up like gallery/picture/157
you can call that number with either $_urlvars[2]
or $urlvars['picture']
. This not only gives you some flexibility to move things around your URL but makes it much easier to grab a specific variable when you down on like 876 of the project.
Now back to line 2, this one is very optional and only has use if your doing specific things. What line 2 does is rename the core variables. For example if you had a URL like this category/0/books
while your URL would be neat if you were using $_urlvars[0]
to tell your application what section to load it would all the sudden find itself with books
instead of category
, throwing the whole site outta-whack. The second line converts all the original strictly numeric array items with relative ones all starting with an underscore. In your script you simple use $_urlvars['_0']
in place of $_urlvars[0]
and even if a 0 pops up in the URL somewhere else your still going string. Of couse if _o turns up in the url you have problems again, so if that may happen you’ll want to change what you make it, I’ve been using a single underscore because that will never occur in my URLs, so it’s safe for me.
Wrap Up
Thats really it. Super simple and straight-forward. There is an example of this in action so you can see how it all works out and all the information you get here: Perfect .htaccess PHP rewrites example . I also have all the code here up on a Snipplr Snippet (It would be if it dident go down as I tried to post it) and a gist (gist:3321959). If you have any questions you can leave them here or on the relevant Google + Post.
As a side note version 3.0.0 of the pgi-inventory-plugin was released today, I posted about the initial release but probably won’t be making a post about the new one (I have plans for handeling plugins a different way).
Also, this new code highlight, it’s pretty sweet, may end up going back and updating a few of the older posts to use it.