Mar 18 2009

Guest Post: .htaccess tips and tricks!

  • (24) Comments. Got a say in it?
  • Published March 18th, 2009 in General by Dan
Learn the techniques I used to make over $4,000,000 in affiliate commissions! Check out my Free Affiliate Marketing Guide and learn the "how to" for Facebook Ads, PPC Affiliate Marketing, PPV / CPV Affiliate Marketing, And Media Buying!

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!

  • (24) Comments. Got a say in it?


Before you act upon this post, read this disclaimer.

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

  1. [...] always a good read, I strongly suggest you check it out – and of course don’t forget to check out my guest post! :) Read the RSS [...]

  2. Jon,

    Great tutorial but still daunting for someone who is not technically literate. Rewrite rule requires knowledge of regular expressions and what they are. But great post nonetheless. keep it up.

    Jon, If you don’t mind can I leave a URL to my PHP tutorial site here. I created this PHP tutorial site in 2006. It’s under different owner now but still a great place for someone to learn PHP.

    http://php-learn-it.com

    -G

  3. CPU says:

    Hello! I am from Belarus. Therefore, I am sorry for my bad English.

    I have been reading your project jonathanvolk.com via translate.google.com. Now I want to help you. … Yesterday, I learned that you have the plugin, which is a serious error. Plugin – Top Commentators Widget. Error in plugin allows any user to take a place in the list of active commentators. To do this, leave only 1 comment! To this end, the visitor must:

    1. leave a comment
    2. specify any name that is already in the LIST Top Commentators (eg, Daniel),
    3. link to your site

    Now the list of Top Commentators, under the name Daniel will be a link on someone else’s site.

    Detailed, I described here – http://www.vse-ok.com/2009/03/17/bag-v-plagine-top-commentators-widget-dayot-eshhyo-odnu-lazejku-dlya-optimizatorov

  4. Great writeup, not sure if it’s mentioned but always make a copy of your .htaccess before you begin to edit it, if you make undesirable mishaps you can always just go back to the original.

  5. Great write! Looking forward to it.
    Thanks Jonathan!

  6. Dan says:

    Thanks for the reviews, think I’ll follow this up soon with a part two :)

  7. mona says:

    Hmmmmmmm
    well i hope it is a nice post but honestly i didnt understand much please make it small and to the point Thanks

  8. Kai Lo says:

    This is amazing! This stuff should definitely be on Digg. These URL changes are much SEO friendly.

  9. Oleg says:

    Good information. It was hard to find all this stuff about .htaccess in one place when I started long time ago)

  10. webkinz says:

    Thanks for such useful info!

  11. Christopher says:

    This is a great post. There’s so much power you have with .htaccess and it is surprising how many people don’t take advantage of it. It’s really ashame.

  12. The .HTACCESS file is on just about every single installation of the Apache web server and can allow you to quickly and easily hide your affiliate links so that they not only look more attractive, but they can stop “affiliate thieves” from steeling your “hard earned” affiliate commissions.

  13. Sancho says:

    Good site, admin.

  14. Wow, you shared us the tips which is difficult to find anywhere else. I definitely look at tips.

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>