Posts Tagged ‘Apache’

Easy Apache VirtualHost config

March 15th, 2009 1 comment

Like most of the word I use Apache to serve up both my live and test websites.

The downside to having a box for development though is the constant config of apache to host these sites, most of which require absolute links (e.g. ‘/css/style.css’) to function.

The Apache snippet below sets up dynamic sub-domains which can be changed without reloading Apache every time you add, remove or update anything something. Simply drop your sites inside /var/www whenever you want a new subdomain. Alternatively you can symlink if they are hosted elsewhere on the box.

Its pretty easy to install, simply paste the below in your /etc/apache2/apache.conf file…

# Easy virtual host config
# See

DocumentRoot /var/www/
ServerAdmin admin@localhost
Options +FollowSymLinks -Indexes
LogLevel debug
ServerAlias localhost
ServerName localhost
RewriteEngine  on
RewriteLogLevel 3
RewriteCond    %{HTTP_HOST}  ^localhost
RewriteRule    ^(.*)$        /www/$1 [L]
RewriteCond    %{HTTP_HOST}  ^www.*
RewriteRule    ^(.*)$        /www/$1 [L]
RewriteCond    %{HTTP_HOST}  ^(.*)\.localhost
RewriteRule    ^(.*)$        /%1/$1 [L]

Now add your servers to the /etc/hosts file:      server.localhost

Now any directory you place in the /var/www directory automatically becomes a subdomain. You can now visit http:///server.localhost as a fully fledged webhost.

If you want to use another name than the rather boring ‘localhost’ simply replace the text in both text pastes above.

If only there were a way of removing that /etc/hosts hack it would mean zero config from then on. If anyone has any ideas do let me know. The addition to the hosts file seems to need applying if you are working from the same box the Apache server resides on. If you arn’t and the box is a local dedicated server its not needed at all and domains can be added and removed as necessary.

Categories: HowTo's Tags: , ,

Pretty URLS in CodeIgniter

January 12th, 2009 No comments

CodeIgniter has to be my favorite framework for PHP. The way it keeps out of your way while working to an MVC standard is something deserving of the highest praise.

For reasons passing understanding though the programmers of this excellent system have desided not to provide pretty URL’s out of the box. Getting infomation on this is a little tricky so here is my take on the situation.

Simply dump the following into a file called ‘.htaccess’ (Note the starting Dot) in the root path (thats the one at the very start of your path tree that should contain the ‘system’ folder).

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)$		- [L]

RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

As with most mod_rewrite hacks you will need that enabled on an apache server. Its very rare to find a hosting company that doesn’t use this setup so you should be fine.

You will also need to change the following variable to a blank value in the system/application/config/config.php file:

$config['index_page'] = '';

And you’re done. From now on your Urls are addressed in the form: http://SITE/CONTROLLER/METHOD/VALUE1/VALUE2 (e.g.

Categories: HowTo's, PHP Tags: , , ,

Templating in PHP in 10 seconds flat

December 6th, 2008 No comments

One of the rather messy practices that the PHP documentation actually encourages is the practice of using include or require to correctly layout a web page.

The PHP manual suggests that two pages be created: header and footer, and that these are included at the beginning and end of each document.

Thus for a ‘contact us’ page the contact.php file would look something like:

... The contact pages HTML ...

While this does work, there are easier ways to accomplish the same thing and still maintain compatibility with editors such as Dreamweaver which seems to choke on even a small amount of PHP.

The under-documented auto_prepend_value and auto_append_values get a brief mention in the ini directives documentation but I thought it would be nice to show an example of how these can be used in the wild,

Adding the following to either an existing ‘.htaccess’ (note the starting dot) or downloading the below file will do pretty much the same as the above contact.php file example did only without needing to specify the files in the beginning and ends of each separate file.

php_value auto_prepend_file "header.php"
php_value auto_append_file "footer.php"

And as with all .htaccess directives you can override these on a directory basis. So if you need a different header and footer when inside the ‘invoices’ directory dump another copy of the above into that directory changing it as needed.

And yes this method can be merged with the previous post to work with the URL prettifier.

Categories: HowTo's, PHP Tags: , ,

URL prettification in 10 seconds flat

November 29th, 2008 No comments

A word to all PHP, ASP, Python and Perl web-devs out there:

Is soooo 1990’s.

URL prettification has always been seen to be the exclusive domain of larger frameworks but with Apache it really isn’t that hard.

Simply paste the following into a file named ‘.htaccess’ (note the starting ‘dot’) and dump it in your website to allow the extension of your script to be ‘guessed’ by Apache should it not be provided.

This hack obviously requires mod_rewrite but since every hosting provider and their dog provides this these days i would say not having it enabled would be the exception rather than the rule

RewriteEngine On
RewriteBase /

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

ASP, Python and Perl people either add your .asp, .py or .pl sections under or above the .php bits or simply replace them.

There, now we can use:

But the old way (with .php extension) is still valid too.

Finally we put an end to this silly file extension nastiness.

Categories: HowTo's, PHP Tags: , ,