The .htaccess
file is one of the most powerful tools available on your Apache server (this only works on Apache, so Windows users must find their fun somewhere else). It allows you to provide a way to make configuration changes on a per-directory basis. In this tutorial, I want to go over how to make pretty URL’s using this file.
Pretty URL’s are not the only thing the .htaccess
file is good for, you can also control authorization, blocking, MIME types, and more…
Before we get started, please note the following warning from Apache:
You should avoid using
.htaccess
files completely if you have access to httpd main server config file. Using.htaccess
files slows down your Apache http server. Any directive that you can include in a.htaccess
file is better set in a Directory block, as it will have the same effect with better performance. – Apache.org
Pretty URLs
Let’s saying you have a URL that looks something like http://yoursite.com/?page=member&id=1
. This is obviously not a very good looking URL. Something like http://yoursite.com/member/1/
would be a lot more desirable.
One of the biggest advantages to pretty URL’s (other than the fact they’re pretty) is they are much more memorable for visitors to your site. They are also more likely to be crawled by web spiders (which I go over in my SEO Tips & Tricks for WordPress post). Some web spiders will avoid ugly and convoluted URL’s.
Finding the .htaccess
File
Now that we know why we want pretty URL’s, let’s take a look at how to actually get them. FTP into your server and navigate to your site’s main directory. This directory will most likely be called httpdocs
. Once here, check to make sure you can see invisible files. You can then open your .htaccess
file, or if you don’t have one yet, create one.
.htaccess
Commands
The first thing we want Apache to do is recognize we want to create our own rules. To do this, you just need to enter the following command as your first .htaccess
file:
This tells Apache we want to make our own rules.
Next we want to take a look at forwarding a link. For the sake of this tutorial, I’ll assume all incoming requests are going to be handled by index.php
using a get variable to tell us what page to retrieve. For example, if we want the member page, the request would be [startcode]index.php?page=member, so the URL would look like http://yoursite.com/?page=member
.
To tell Apache to forward our pretty URL to our index.php
file, we use the RewriteRule
command. This command has two parts. First is the format of the pretty URL followed by the actual place the URL will go on the server. For our example, we would use this:
Let’s break down the command above piece by piece. First, we have the call to the RewriteRule
command. This tells Apache we want to start a rewrite. Next we have the format of the pretty URL, which looks like ^([a-zA-Z0-9]+)/$
.
The carat symbol signifies the base URL, in this case http://yoursite.com/
. This would change depending on what directory you put your .htaccess
file in.
Next we have a regular expression surrounded by brackets. If you aren’t familiar with regular expression, there is a great online tool to test them out located here. Basically what this one is saying, is it will accept any combination of lowercase letters (a-z
), uppercase letters (A-Z
), and numbers (0-9
).
Finally the $ tells us the end of the pretty URL. The next part of the command is the file we want to go to from the pretty URL, in this case index.php?page=$1
. You’ll notice that we have a $1
in there. This is a variable from our pretty URL. Each variable corresponds to a regular expression. Since there was only one regular expression in this command, the $1
represents that.
Let’s walk through an example. If we type in the URL http://yoursite.com/hello/
then $1
will be equal to “hello”, so we will retrieve the information at index.php?page=hello
.
You might have noticed that we have a trailing /
in the section for our pretty URL. This does mean that if you don’t type in the /
then the rewrite tool won’t work. However, we can easily remedy this by adding one more rewrite rule for that case.
Adding On More Rules
Once you have the basics down, you can start adding more rules. A full .htaccess
file may look something like this:
You can see that the root is redirected to a login page, and that there is now room for two variable calls. This would start leading into things like RESTful URL’s, where you could have http://yoursite.com/member/
list all the members, but http://yousite.com/member/1/
list a member with the ID of 1, which could be retrieved by index.php?page=member&id=1
.
Of course all of this isn’t just magically managed. You would have to create the code that reads the get variables, includes the proper files and figures out what its supposed to do. But you can get some very powerful things done with this.
Conclusion
I wrote this article pretty fast, so if there are any mistakes or inconsistencies please let me know in the comments below. I just started to learn more about the .htaccess
file myself, so if there are any other tips and tricks out there I’d love to hear them.
As always thank you for reading and please share it around as much as you can!