Guest Post: .htaccess tips and tricks!

Wednesday, Mar 18th, 2009 by Dan

Who's the guest?

Dan Walker runs his blog over at Dans Blog, he's a great PHP/MySQL coder and startup affiliate himself. He tends to share a lot of his technical knowledge with affiliates less techy, this is his first guest post of hopefully many :)

Welcome

Welcome to this tutorial on useful tips and tricks that you can do with htaccess! This tutorial will only cover the basics and essentials, a more advanced one may come in future. First of all a quick primer on what htaccess actually is, for those of you who haven't heard of it or are unsure of what it does.

htaccess is a configuration file used by most web hosts to configure how the URL's look and how the website actually behaves. For an affiliate marketer in particular it can be the difference between:

http://www.jonathanvolk.com/blog/?somepage=readpost&postid=my_first_post&this=that

and the much more friendly-on-the-eyes version:

http://www.jonathanvolk.com/blog/my_first_post/

As you can see, htaccess makes URLs much more user friendly and easier on the eyes. The prettier URL is nicer to look at, it better explains where the user is, what they're reading and even better for marketers - they're more likely to remember it. Not only that but more friendly URLs are easier to manipulate and users can use them to navigate your site in some cases. Consider this for example, the user is on a page about cards made by Honda, but they'd much rather look at cars made by Ford. Here's your two possible URLs:

http://www.jonathanvolk.com/vehicles.php?car_brand=honda&perpage=20&somevar=this

http://www.jonathanvolk.com/cars/honda/

I exaggerated the top URL a little but the point is there, the second URL is much better from all aspects, it also helps for SEO as it's almost describing the page, extra brownie google points.

Getting a bit more technical

Right, now we know what htaccess does, let's get a little bit more technical. To make use of this wonderful little useful piece of SEO gold, we need to be using an Apache server. It doesn't matter if we're running Windows, Mac OS, Linux, Sun OS or anything in between, as long as we're running apache. The next thing we need is the Apache module mod_rewrite to be enabled, if you're unsure then check with your web host.

htaccess is a file, basically whatever directory you put it in, the rules will apply to that folder and all the sub-folders. So if you put it in /public_html/ then it will apply to all your web-accessible folders. Right, let's crack on!

Let's get started

Ok, go to whatever folder we're testing on, for these examples I'm just going to put my file in public_html so it applies to my entire website. The file is called (amazingly) '.htaccess' without the quotes. The '.' prefix to the file means that it's a hidden file, so if you try to create a new file and it says it already exists, make sure your FTP is set to view hidden files. Now open up the .htaccess file to edit, and we begin!

Basic URL rewriting

Ok so the first line you're going to want to put is;

RewriteEngine On

This enables the ability to transform the URL in real time, if this was off it wouldn't work. For our first example let's turn this ugly URL:

http://www.jonathanvolk.com/index.php?page=contact

In to something more sexy...

http://www.jonathanvolk.com/contact

I know which one I'd rather take on a date. Now let's find out how to do it. The code for this is pretty simple, it looks like this in total:

RewriteEngine On
RewriteRule ^([^/]+)/?$ /index.php?page=$1 [L]

Ok, now for simplicity let's break that down into parts. First of all what is RewriteRule? It's a simple command that we write when we're going to write a new URL changing rule, like converting index.php?page=contact to /contact.

Second, what is ^([^/]+)/?$? It's a regular expression. I'm not going to go in to great detail with regular expressions because they're beyond the scope of this tutorial and there are plenty of great tutorials on Google. For now, all you need to know is that ^([^/]+)?/$ will match any string, be it /contact /about or anything else similar. If you know regular expressions (regex) great :)

The second parameter /index.php?page=$1 is the old URL aka what the new URL is doing behind the scenes. The $1 is replaced by whatever is in the brackets in the first parameter. So if a user typed in /contact then $1 would be replaced by 'contact'.

Finally the [L] in the square brackets tells mod_rewrite that once this rule has been used it should be the Last rule used. It's typical in files like htaccess to have many rules that could interfere with one another, by using L after you've applied the rule htaccess will not read any more RewriteRule's.

Passing more than one variable

Let's say your script takes more than one variable, let's say your URL looks like this:

http://www.jonathanvolk.com/car.php?make=ford&model=mustang

This is quite an ugly URL, lots of unnecessary variable names and ampersands (&) that we can get rid of. We want it to be pretty, so our new htaccess file looks like this:

RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/?$ /car.php?make=$1&model=$2 [L]

$1 will take the make, $2 will be the model. Which means our new, better URL will look like this:

http://www.jonathanvolk.com/ford/mustang

Beautiful. You've just covered the basics of RewriteRule, you can replace what's in those brackets with any regex you like and you can customize the URL to look how you want, let's move on now to look at other useful stuff with htaccess.

Forcing the trailing slash

For those perfectionists out there like me, don't we just love consistency? In this case, consistency is a trailing slash on the end of every URL. To accomplish this we use this code:

RewriteEngine On

RewriteRule ^([^/]+)/([^/]+)/$ /index.php?make=$1&model=$2 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ http://jonathanvolk.com/$1/ [R=301,L]

Hey look, new stuff! What's a RewriteCond? Basically it specifies a condition for which the following RewriteRule will take place. The first two RewriteCond's we have up there are followed by !-f and !-d, the f means if the user requests a file, don't serve index.php and the d is the same just for a directory. The 3rd line is for some matching with your URL, don't worry too much about that right now! The most important line is the last, the rule, which puts anything it found after your URL where $1 is, forcing a trailing slash (because Google likes it when our page only has one URL).

Forcing www or non-www

If we want even more Google points, we know that Google loves it if we only have one URL to avoid duplicate content in the search engines. We're half way there with the trailing slash, now we need to decide whether to force a www or a non-www policy. It's down to personal preference really but in my experience I tend to force no-www.


RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.jonathanvolk.com$
RewriteRule ^(.*)$ http://www.jonathanvolk.com/$1 [R=301]

This would force a www, if we wanted to force non-www then simply remove the 'www.' from both lines :)

Conclusion

Whew, will this post has gone on a little longer than I expected! However we've still only lightly skimmed on the basics. Further interesting topics include stuff like making everyone but you see a maintenance page (useful for updating), blocking directory listings, custom 404 pages and much more!

Depending on the reviews for this post, I might make a part two, it's up to you guys :) If you have any questions post them below and I'll try my best to answer!

I'd like to thank Jonathan for giving me this wonderful opportunity to post on his blog, it's a great blog and one that I read on a daily basis and it feels great to contribute to it!

Tags:
Posted in General
500+ CPA Networks Reviewed

24 Comments to “Guest Post: .htaccess tips and tricks!”

Leave Your Comment

Your Gravatar

Don′t see your avatar?

Go to Gravatar.com and create one!