"Clean" urls with php

So... everybody talks about how they want "clean" urls - in other words use pathinfo instead of query strings and leave off the extensions (.gif|.png|.php|.html et al) These are all methods that assume you're aware of how to mangle pathinfo to get the information you want, they don't rewrite urls into query strings (although that's the method most things, like wordpress, seem to take) So I've done some rather exhaustive research and here are my wonderful findings...

There are several ways of creating "clean" urls for php scripts
Each method is listed below along with implementation instructions and gotchas
Let's envision we have a webroot of /htdocs and we're trying to serve /htdocs/index.php to a user
from http://mysite.com

## 1. Semi-Clean Urls - Default version
## By default any php script can work with semi-clean urls in the format
## http://mysite.com/index.php/user/login, you shouldn't have to make any changes
## to your server setup.
##
## If you run into problems: On IIS php must be installed WITHOUT the "check if file
## exists" option checked, in Apache2 you may need to add the line below to an .htaccess file or in your httpd.conf
#
# AcceptPathInfo on
#

## 2. Error Document - Servers with Configurable Error Docs
## This uses custom error document to auto redirect to index.php
## The only DOWNSIDE to this is it WILL flood your error log with bogus
## 404 errors - it's HIGHLY recommended that you disable (error) logging
## This method involves setting index.php as a 404 error document
## read your server docs for more info
## set index.php as your custom 404 error page (use url method for IIS)
## This allows access to your site WITHOUT the index.php/index so you have very
## clean urls - http://mysite.com/user/login - This method CAN be used with
## other methods and is the ONLY method that works on IIS
## WARNING: you'll have to take a look at what $_SERVER sends and adjust your pathinfo
## finding routines accordingly - IIS can do some nasty stuff but the info IS there
# ErrorDocument 404 /index.php

## 3. Multiviews - Apache
## This is the quickest, easiest way to set up clean urls on apache
## Apache MUST have mod_negotiation enabled for this to work
## Uncomment the line below to active it - no other step is required usually
## This method, however, requires you to add index into your url, you may also need the additional line for defaulttype
## It's said that some browsers will give 406 errors and to get around it use AddHandler instead of AddType to set
## up php, but I've personally never have a problem. Urls will be http://mysite.com/index/user/login
# Options +Multiviews
# DefaultType application/x-httpd-php

## 4a. ForceType - Apache Module
## This is also a fairly easy way to set up clean urls, uncomment
## the three lines below AND remove the .php from the name of the file index.php
## Apache must have mod_mime enabled and php cannot be run as a cgi, it must
## be run as a module, urls must have index - http://mysite.com/index/user/login
#
# ForceType application/x-httpd-php
#

## 4b. ForceType - Apache CGI
## You CAN use forcetype with php as a cgi on apache, HOWEVER, it requires
## a little more setup. First of all, you can either uncomment the lines below
## or make sure they're in your httpd.conf correctly, remember the path to your
## php binary must be changed, urls must have index - http://mysite.com/index/user/login
# ScriptAlias /php/ /path/to/where/php/binary/
# AddType application/cgi-php .php
# Action application/cgi-php /cgi-bin/php.cgi
## Then uncomment the forcetype directive below and change index.php to just index
#
# ForceType application/cgi-php
#

## 5. Rewrite Engine - Apache
## This is the last apache only method and uses mod_rewrite to write the (index).php
## into the url, this is the most resource intensive but the most complete and should only be used
## on a server that can handle it, to activate uncomment the lines below
## urls can be in ANY format - the regex won't rewrite ones that already have index.php
## WARNING - DO NOT USE ON VERY LARGE SITES OR SITES WITH LOTS OF HITS...MOD_REWRITE IS A HOG
## AND CAN CRASH YOUR BOX REALLY EASY BY EATING MEMORY AND RESOURCES - you've been warned
# RewriteEngine on
# RewriteCond %{REQUEST_URI} !(index.php)(.*)$
# RewriteRule ^(.*)$ index.php/$1

## There are commercial solutions for rewriting IIS urls if you don't want to use the document error method

Comments

Be the first to write a comment!

Post a Reply