Posts Tagged ‘Apache Web Server’

Setting up virtual hosts in apache on a windows machine

Saturday, February 21st, 2009

Navigate to your apache folder and find the conf folder (for WAMP this is C:\wamp\bin\apache\apache{version number here}\conf\ by default, for xampp this is C:\xampp\apache\conf\ by default)

Next open the httpd.conf file, and search for the line that has Include conf/extra/httpd-vhosts.conf on it. If there is a # before the line, delete it leaving only Include conf/extra/httpd-vhosts.conf. Save the file and exit it
Next, open the folder extra in the conf folder, and open the file httpd-vhosts.conf
You can delete the contents of the file, and replace with the following

NameVirtualHost 127.0.0.1

	DocumentRoot "C:\wamp\www"
	ServerName localhost
    
        Options FollowSymLinks
        AllowOverride All
    

For my example above, I have used the WAMP default document root for the localhost (C:\wamp\www), as it is set by default. You can change the paths to any directory you like, making sure that both match each other. Now you will need to create a custom virtual host. Supposing you want to make the host myhost, so that typing in http://myhost/ brings up a new document root by default, you would add the following to the file after the above code


	DocumentRoot "C:\virtualhosts\myhost"
	ServerName myhost
    
        Options FollowSymLinks
        AllowOverride All
    

Note that there are three parts that are different to the first one. The first two differences are the folders (in this case I’ve set them to C:\virtualhosts\myhost). The second difference is the ServerName which is myhost

Now we need to make a final change to our hosts file. The location of the hosts file is dependant of your operating system. A full list of these can be found at http://en.wikipedia.org/wiki/Hosts_file#Location_and_default_content

Most windows users will find it in %SystemRoot%\system32\drivers\etc\
Right click the file, and click properties. Make sure that the Read-only checkbox in the attributes is unticked and click ok. Then open the file for editing (using notepad is generally the easiest method)

We will need to add our new myhost to this file to allow the computer to map the name to an ip. Since the IP will be our local ip (127.0.0.1) we simply add
127.0.0.1 myhost to the end of the hosts file on a new line (the gap between the ip and the myhost is a tab by the way). Save this file, close, and restart your apache server

You should now be able to go into your browser and type in myhost, and the C:\virtualhosts\myhost folder will run like your normal document root does

If you have any issues in getting this to work, feel free to contact me

How to enable mod_rewrite in Apache

Sunday, June 22nd, 2008

To enable the rewrite module in Apache you will need access to the httpd.conf file, located in {Apache folder}/conf/httpd.conf
Open up the file, and search for
#LoadModule rewrite_module modules/mod_rewrite.so
and simply remove the # from the start of the line. Then save the file, and restart the Apache server. You should now be able to use the mod_rewrite functionality on your server. to test this, create a new file called test.php in your root html folder, with the following code in it

and put

RewriteEngine On
RewriteBase /
RewriteRule ^test/(.+)$ test.php?p=$1 [L]

into a .htaccess file in the same folder

Don’t forget to delete these two files after testing

Making dynamic/pretty URL’s for PHP scripts

Friday, June 20th, 2008

We’ve all seen sites where the URL looks really ugly, i.e.
example.com/product.php?id=12345&action=purchase&num=2

However, over the last few years, more and more sites have converted to dynamic/pretty URL’s, where it looks more like example.com/buy/12345/2/

Using some simple regular expressions and the rewrite module of apache you can create pretty page URL’s

So we have three sections, clearly coloured below:
example.com/buy/12345/2/ need to map to
example.com/product.php?id=12345&action=purchase&num=2

Here’s the RewriteRule for this example, which should be put inside your .htaccess file

RewriteRule ^buy/(\d+)/(\d\d?)/?$ /path/to/public_html/product.php?id=$1&action=purchase&num=$2 [L]

This is a case sensitive search, ie, /BUY/ would not work. If you wish to make it case insensitive, change the [L] to [L,NC]
Note: If you haven’t already got

RewriteEngine on
RewriteBase /

in the top of your .htaccess file, you will need to add it. The first line is required to get the rewrite engine working, and the second is to start matching the URL after the / on the end of example.com/