.htaccess - The Gift and the Curse

05 August

.htaccess - The Gift and the Curse

Now we recently came across a situation where we had to work on changing some stuff around on a website. Since the website was already established and had inbound links and such, we could not just start all over with writing a new system. So we decided to do what we needed to do using an .htaccess file.

Holy cow!!! where is some legitimate documentation on this? tell me please? Well we did end up finding some good information on it via cheatsheets like Addedbytes.com, LifeHacker.com and a Google Group. I know there were more, but for the most part we kind of tried to stay in these sites.

So from what I have read; and correct me if I'm wrong, the .htaccess file needs to be layed out in a particular order. This makes it seem like one of those slider puzzles you used to get in the cheap candy bags, where you needed to have everything layed out in a certain way, in order to actually see the picture for what it is.

Actual example from the project:

So we had a structure like this

.com/blog.php?id=444 (page was called, id would return the information for that blog)

They wanted:

.com/title-of-article/

So where to start? After gathering a bunch of cheat sheets and references and searching...we decided to do this.

RewriteRule ^([A-Za-z0-9-]+)/?$ blog.php?title=$1 [L]

so here is the basic gist. the ^ is the site (i.e. http://www.your-domain.com/)
the ([A-Za-z0-9-]+)/?$ basically breaks down to "it dont matter if its a letter number or dash and ends it with the $.

the rest tells the server that anything that does ([A-Za-z0-9-]+)/?$ i.e. .com/title-of-article will return the page: blog.php?title=title-of-article

nothing to it right? woah...hold your horses there cowboy. now if your cms was only returning information on the blog using its unique ID, then how would the system know now with the title? IT WONT!

we also had to change the function that returned the blog information to return the blog information NOT by the id it has, but by the title. granted that your system already added a SEO-Friendly title to the DB that you can query.

If all those things are in place, then you should be good to go.

Now I know the title might not have made sense, but note that if you have some information and know what and how to work with the .htaccess file, is a gift. If you have never worked with them and only seen them in passing, then good luck to you pal! read up. But once you understand the basic concept you should be ok. uhm yea..ok.

TAGS: Web Development, What's Trending

Share this post!