William Jiang

JavaScript,PHP,Node,Perl,LAMP Web Developer – http://williamjxj.com; https://github.com/williamjxj?tab=repositories

web server .htaccess

Web Server .htaccess routing example

For a PHP micro-framework and quick solution, using web-server’s (like Apache) URL rewrite(mod_rewrite) is a good choice. Just add/edit .htaccess file in specific/various dirs to control the URL routines. A standard .htaccess file:

RewriteEngine On
RewriteBase /name/

RewriteCond %{DOCUMENT_ROOT}/name/$1.php -f
RewriteRule ^([^/]+)/([^/]+)/?$ $.php1?action=$2 [L,NC,QSA]

RewriteCond %{DOCUMENT_ROOT}/name/$1.php -f    
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ $1.php?action=$2&id=$3 [L,NC,QSA]

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteRule ^(.*)$ $1.php [L]

This will convert a php HTTP request to a better meaningful URL:

//From HTTP Request:
routing.php
routing.php?action=create
routing.php?action=list&id=1

// To:
/routing
/routing/create
/routing/list/1

In this way, PHP doesn’t involve into routing request. Compared to PHP MVC framework and routine, it is faster, but less extensible and flexibility. A good URL rewriting example from CakePHP is at:

http://book.cakephp.org/2.0/en/installation/url-rewriting.html

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: