The basics of Mod_Rewrite

December 31, 2007 · Filed Under Tutorials · 2 Comments 

 

Friendly URL’s are nice. Nobody really likes copying or trying to type urls like
http://yoursite.com/folder/do.php?do=rk2&cat=3
When you could just have
http://yoursite.com/folder/do/3/rk2
It looks better; it’s search engine friendly, and it’s pretty simple to do on an apache webserver! Rewriting your urls also hides the underlying technology which powers your site; which is good for some, and the links look much more permenant.
First Steps
ReWriting is done using the .htaccess file in your home or root directory, normally /var/www or public_html ; depending on your setup. You should make the file on your local computer as htaccess.txt, then upload it and change it’s name, because it has a tendancy to vanish once it’s uploaded, so you can’t download it again.Now, you need to activate mod_rewrite for your website, as in some servers it isn’t enabled by default. Provided it’s installed with Apache, add the following statement to the top of your .htaccess file, before any rewrite rules, and you’ll be fine.RewriteEngine OnNow, provided that file is in your root directory, mod_rewrite will be enabled.
Basics
We will begin by rewriting one static url to another; for demonstration purposes. We are going to visit page1.html and the webserver is going to show page2.html transparently.
RewriteEngine On
RewriteRule ^page1\.html$ page2.html
This is the simplest example there is, but it may throw some people off; so I will explain it:* The Caret (^) signifies the start of the URL to be matched, under the current directory (the one that the .htaccess file is present in)* The $ signifies the end of the string to be matched by Apache; after it comes the new string.* The backslash before the . has a special purpose, too. The . or full stop, is a special character to Apache; and the \ tells it to ignore the key and continue processing the rule, leaving the . as we see it.And there we have it. With that rule, when a user visits page1.html ; the webserver will load page2.html without them knowing they’ve been redirected!If you want your users to know they have been redirected; add [R] to the end of the line, and then they will be redirected fully, not transparently.
More Advanced Application
Now we get to where mod_rewrite becomes really useful; when you start converting friendly urls into scripts. It’s not complex, and a single rule can define redirects for a whole directory; provided that they use the same pattern, (ie A-Z, 0-9 and a-z). For beginners, this can seem pretty complex, but once you get the hang of it, you’ll find that it’s well worth the effort Here is an example of this sort of application at work::
RewriteRule ^go/([0-9][0-9])/$ /go.php?goid=$1
This rule will match URL’s that start with go/ and are followed by any 2 digits, and a trailling / on the end. For instance, it will match go/12/ and go/98/ , and load the php page go.php?goid=12 or 98 , completely transparently.Now, explaining it. The square brackets are shown to contain the range, in this example it’s any 2 digits, and you can copy each section as many times as you like. You can also use [a-z] for lowercase letters; [A-Z] for uppercase letters and [A-Za-z] for any letters.The “regular” part of the string is encased in parenthesis, ( ), because we store whatever value is found here for later use. Once there is a value in a parenthesis, we can use it through what’s called a back reference. Each of the pieces of information you have placed in the parenthesis are assigned an index, starting with 1. So; the first back reference is $1, third is $3; so on and so forth. You can then recall them in the “rewrite to” section; like I have above, with $1.Once that’s done, the visitor can visit /go/12 and load the page go.php?goid=12 ; without knowing it, and without seeing the real url.The biggest problem with that is that if your visitor goes to /go/12 without the trailing / , the rule won’t recognise it, and they will just get a 404 error, so you then want to add a [R] rule which will add a / to the end, like so:
RewriteRule ^go/([0-9][0-9])$ /go/$1/ [R]
before you add the previous rule, so this one is processed first. Then, when they visit /go/12 ; they will be redirected to /go/12/ and load the php page specified earlier.
Increasing the String Length
If you have a match term longer than 2 digits, for instance if your product is a word, like /products/awesomehat ; then you can use a symbol on the match to allow it to match an infinite length of that character, so /products/a/ would work, and so would /products/thisisareallylongproduct/It’s like telling Apache “one or more of the preceding character or range”::< br class="webkit-block-placeholder" />RewriteRule ^products/([a-z]+)$ /products/$1/ [R]
RewriteRule ^products/([a-z]+)$ /product.php?prodname=$1
And that’s it. Now, anything entered after /products/ that uses lowercase letters will be picked up by the rewrite engine, and will correspond to the PHP file, so visiting /products/thisisaproduct will load product.php?prodname=thisisaproduct ; exactly as it did before, but the length is now unlimited.I hope this helps you with Mod_Rewrite, and if you have any problems, please contact me You may not copy any part of this tutorial without express prior permission of myself.Thanks for reading

Installing Apache, PHP and MySQL on Ubuntu

December 31, 2007 · Filed Under Tutorials · Comment 

 

This tutorial is designed to help you install the LAMP (Linux,Apache,MySQL,PHP) combo on your Ubuntu machine.

What You Need

Ubuntu (Obviously)
Root Access Password
About 7gigs HDD space (ish)

Step 1
You firstly need to install Apache 2 onto your machine. Fire up the terminal, and enter:
sudo apt-get install apache2
It should then install automatically.
To start Apache, use the following command:
sudo /etc/init.d/apache2 start
And to stop it;
sudo /etc/init.d/apache2 stop

You should then be able to goto http://localhost , and see some default text, something like “It works!”

Step 2
Next up is PHP, enter the following code in terminal:
sudo apt-get install php5 libapache2-mod-php5
Then restart Apache;
sudo /etc/init.d/apache2 restart ;
and PHP is done. You can change PHP5 to any version you like , but PHP5 is recommended.

Step 3
Next, we install MySQL. It’s also very simple, run the following in terminal:
sudo apt-get install mysql-server
And then you need to set the root password for MySQL (important)
mysql> SET PASSWORD FOR ‘root’@’localhost’ = PASSWORD(’password!’);
And that’s MySQL In. Restart Apache again;
sudo /etc/init.d/apache2 restart ;
And it should all work nicely!

An excellent tool for PHP is PhpMyAdmin, which can be installed with the following:
sudo apt-get install libapache2-mod-auth-mysql php5-mysql phpmyadmin

And that’s it! All done. If you have any problems with PhpMyAdmin, try rebooting apache.

Have fun with LAMP!

–George

Free Adbux Clone Script

December 31, 2007 · Filed Under Ramblings · 4 Comments 

To celebrate the re-creation of GPearce; we will be giving away the adbux clone script, for free. It was purchased by us with full resale rights, and we are using those rights to give it away.If you visit adbux.to ; you will see the script in action, you can make huge money from it, and it’s entirely free.

For a demonstration of the script itself, visit y-fusion.com.

The script is fully customizable, you can change how it looks very easily, and it sells for as much as USD10 on some websites.

Please note this is subject to our disclaimer:

1- We are not responsible for anything that happens because of this file, profits, losses, damages or suchlike

2- We do not host the file, just link to it, which we have the right to do.

3- If there is a problem with this, just email us and it’ll be removed.

4- As nothing has been paid for this script, compensation for anythign that happens because of it will not be paid or requested.

We are also completely entitled to change the terms of use of this script at any time, and you are obliged to abide by them. You are also obliged to abide by the mainstream terms and conditions of use of the GPearce website as a whole.

To download the file, click here

Welcome to GPearce

December 31, 2007 · Filed Under Ramblings · 1 Comment 

Hey There, and welcome to GPearce, my blog. It’s my new years resolution to blog, amongst other things, and this is my platform (:

The aim is to average 1 post per day, which comes to 365 posts this year, I’ll find out whether I managed it soon :) (soon-ish)

Is This Thing On?

December 30, 2007 · Filed Under Ramblings · Comment 

*tap tap*

Hopefully, GPearce is online now. Yay!