Archive for the ‘Uncategorized’ Category

Apache Re-Writes in .htaccess

Sunday, March 29th, 2009

apache logo
In this post I will walk you through an example scenario that will use an apache re-write to clean up the URL to make it more SEO friendly and human readable, which as I briefly touched upon in my last post, is very important for the search engine optimization of your website.

Scenario

Members Site – Profiles

Lets say our members site has 4 members, each of which has their own profile page, containing information stored in a database. To access this page we want to be able to use the URL:

www.example.com/profile/[username]

The Set Up

Database

If we set up a table in our database and create the following table: -

database

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
--
-- Table structure for table `users`
--
 
CREATE TABLE `users` (
  `id` int(3) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) NOT NULL,
  `age` int(3) NOT NULL,
  `tel` varchar(13) NOT NULL,
  `location` varchar(50) NOT NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `username` (`username`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
 
--
-- Dumping data for table `users`
--
 
INSERT INTO `users` VALUES(1, 'stew', 22, '0154684874', 'Doncaster');
INSERT INTO `users` VALUES(2, 'Oli', 24, '01132648745', 'Leeds');
INSERT INTO `users` VALUES(3, 'Craig', 24, '0208548765', 'London');
INSERT INTO `users` VALUES(4, 'James', 21, '01777648465', 'Retford');

And create a very simple profile page:

Profile.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
	<?php
 
	//Set up our database connection	
	mysql_connect('localhost', 'root', 'root');	
	mysql_select_db('example');
 
	//Get the username variable from the URL and asign it to the variable user	
	$user = $_GET['username'];	
	$query = "SELECT * FROM users WHERE username = '$user'";	
	$result = mysql_query($query);
 
       //while there are values within the results array, make them equal to $x
	while($x = mysql_fetch_array($result)){		
		echo "<h1>". $x['username']."</h1><br>";
		echo "<b>Age:</b> ". $x['age']."<br>";
		echo "<b>Telephone:</b> ".$x['tel']."<br>";
		echo "<b>Location:</b> ".$x['location']."<br>";	
	}	
       //close the mysql connection
	mysql_close();
 
	?>

If you visit the page profile.php you will see that nothing appears, because we have not fed a username into the url. If you add the following to the end ‘?username=’Stew’, providing there is an entry in the database for a Stew, a profile along with some basic information will appear on the screen.

screen print

But this URL is not human readable, user friendly or SEO friendly, so we now have to trick our web server into thinking they are seeing this URL. We can do this using a simple .htaccess file located in the root folder of the website. The file will tell the server that when it see’s the clean URL, what it is actually seeing is in fact the dirty, ugly URL, leaving both parties happy.

Ensuring mod_rewrite is enabled

We just have to make a quick check to ensure that your apache server is set-up to handle the re-writes before we start.

In a new file called info.php simply type:

        <?php phpinfo(); ?>

and point your web browser to that page, scrolling down to the ‘loaded moudles’ section, look for ‘mod_rewrite’. If you can’t find it then this site should help you to enable it.

.htaccess

With mod_rewrite enabled we can now write our .htaccess file. Create a new file in your root folder, and called ‘.htaccess’, within the file add the following

1
2
3
RewriteEngine on
 
RewriteRule ^profile/([^/\.]+)/?$ profile.php?username=$1 [L]

RewriteRule breakdown

Testing

With the htaccess file saved, we must first restart appache, then if we visit:

…/profile/James

screen print

you will see the profile on ‘James’ we can change ‘James’ to any of the usernames in the database and the displayed data will change accordingly

Well thats about all for this post/tutorial but please subscribe to my blog for more tutorials

Tags: , , ,
Posted in Uncategorized | 4 Comments »

Apache Rewrites

Wednesday, March 25th, 2009

As many of you will know, the preferred web server for coding in php is called apache. Apache is by far the most popular web server software around, and dominates the web server market place, even though its free :-) !

One of the projects I’m currently working on for Students Work has required me to delve into the world of URL rewrites, which has forced me to look under the hood of apache (all be it only slightly). Now I do have some limited experience in dealing with apache, as I have my own web server set up at home, for a number of personal projects I have as well as a q&a server. So the task of editing httpd.conf files and creating .htaccess files didn’t seem like to much hard work.

So on Monday morning after a brief discussion with James about the direction he saw the project going, I set about creating the rules for the URL rewrites. Now it may be worth mentioning that RegeX is by far my least favourite scripting language, and I positively suck at writing it. I think I must have missed that lecture or been hung-over, one of the two. So I was instantly worried when I looked at the 1st google result contained lengthy and daunting amount of regex, so I quickly closed that page, and searched for the ‘beginners guide’.

Which brought up this fantastically simple, yet complete, easy to follow and very informative guide. The site uses very simple examples to explain the principles of the rewrite, and provides an easy to follow workflow from start to finish.

The end result now means that instead of the search URL having a value:

“/results.php?title=part%20%time&submit=Search”

We get a value:

“/search/part time”

As you can see the new value of the URL is much easier to read and interpret for the user.

Stew

Tags: , , ,
Posted in Uncategorized | 1 Comment »