Monday, March 12, 2012

.htaccess in php

Pretty URLs are increasingly popular. Not only are huge website businesses implementing them, but smalltime web designers are equally striving for that good look in the address bar in your explorer. This article is a follow up based on the comments of my previous story Pretty URLs (How to). I wasn’t planning on this sequel, that’s why part 1 doesn’t say “Part 1”.
I had a complicated scheme the last time around, where I had PHP create new directories and files for me, which would give me pretty URLs, but I’ve now learned that we can do the exact same thing (achieving the same prettiness in the address bar) without having to create directories and files. This time, instead of using functions like mkdir(), we move to a slightly more advanced (but simpler if you understand) method that uses the .htaccess file to translate the ugly URLs into pretty ones. You can even make it so that anybody will think they are browsing through subdirectories when they actually aren’t.
I would like to start by thanking Sven; he really should be writing these articles, but I guess he’s too good for that. We’ll just leave him commenting on everything, which works out great either way.
So, getting started:
Create a file called .htaccess (yes, the name starts with a dot!). You can use notepad or any simple text editor for this. Put this file in your root directory or in a subdirectory if you don’t want it to affect your entire website. The power of a .htaccess file ranges in the directory that it’s in through any subdirectories after that, unless you put in a new .htaccess file that overwrites any of the things you’ve specified.
Now in the .htaccess file you can specify different actions that take place on the server side. This means viewers and search engines will only see the end result. Some of the things you can do are setting permissions for folders or files, password protection, messing around with file extensions, and transforming ugly URLs to pretty ones.
There is a bit of syntax you have to know before you can do what you like, but it’s not too difficult, and most of the time you can get away with just copy-pasting anyway.
# starting a line of comments
\ escapes the function of next character to use it as the character that it is
^ beginning of you matching statement
$ end of your matching statement
. any character
[] a class
| or
() reference point, this becomes a variable
? match previous 0 or 1 time
+ match previous at least once
* match 0 to infinite times
{} match minimum to maximum times ({2,4} match 2, 3, or 4 times)
Then when you are in a class [] there is a bit more:
^ Negation, not
\ same escape character
- used for ranges, for example [a-z]
Now lets do some examples, and you can easily figure it out.
Let’s say we have ralphvandenberg.com with a bunch of ramblings. To access one of these ramblings we need to pass the id as a GET variable, but that makes the URL so un-pretty.
ralphvandenberg.com/ramblings.php?id=34
This gets even worse when we want to pass more variables, and we end up with a bunch of & and = signs in our URL, let alone the ? which is like putting a foot in your grave. So we use the .htaccess file to say that if some pretty URL was typed in the address bar, or used as a link, that it would actually call the ugly URL, but never show any of that.
Start your .htaccess file like this:
RewriteEngine on
Then proceed by putting in your first rule:
RewriteRule ^ramblings/([0-9]+)/?$ ramblings.php?id=$1 [L]
This may look somewhat intimidating, but I’ll go over it bit by bit.
RewriteRule” is just a statement that says what you’re about to do.
^” is where we start the matching.
ramblings/” is the first part we look for. This is static, and makes the pretty URL seem like a folder.
([0-9]+)” gets a number. This is in brackets because it’s the variable we use later.
/?” there might be a slash at the end, and there might not be.
$” the end of the matching rule.
ramblings.php?id=” here is our ugly URL.
$1” this refers to our first bracket set. If we have more bracket sets then there would be $2, $3 and so on.
[L]” closing the line, and finishing the rule.
So now if we type in the address bar:
ralphvandenberg.com/ramblings/35
or
ralphvandenberg.com/ramblings/35/
The server translates that to:
ralphvandenberg.com/ramblings.php?id=35
and returns that page. So in the ramblings.php page you still use $_GET variables the same as before. The only difference is that the URL is pretty! You need to change all your links in your website to suit this, don’t use ?’s anymore!
Now let’s try the same thing for a “fake” directory. We want ralphvandenberg.com/vehicles/cars/ferrari without actually having any of these subdirectories. We have a file called stuff.php where it takes these 3 GET variables.
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ stuff.php?what=$1&type=$2&make=$3 [L]
In the above example, instead of telling the server to look for a-z or A-Z or 0-9 it’s quicker to just tell it to take everything except “/” or “.”.
This opens up the door to a world of opportunities, and potential abuse if you so please. For example you can have you porn site with all the file types being .xxx
Try it out, have fun with it, and do away with all the ugliness!
(note: you may have to configure you server slightly if ModRewrite is turned off)

No comments:

Post a Comment

Thank you for your Comment....

Popular Posts