THAT Agency Design Studio Blog

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.

Cool Slide Puzzle from select-giftshop.co.uk

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.

Share this article

Taking advantage of your .htaccess file can save you in several ways when it comes to running your website. Broken links, bad browser behaviors, changing domain names and unwanted guests are problems we all face. One way to remedy these issues is to take advantage of the .htaccess file when it comes to broken links.

Within the .htaccess file you have the ablitiy to (301) redirect a broken or missing page to a new url or a replacement page at any time by using the code below. The reason this is recommended when removing or moving pages within the site is the search engines tend to keep they’re links regardless of the outdated copy. Especially if your site doesn’t have a high traffic rate or get spidered often. Users coming from the search engines will hit missing or broken pages and get a 404 error and thus not getting the benefit of your site or content.

Search engines as well will hit 404 errors and eventually stop spidering or penalize you for running errors on your site via missing or broken links.

Redirect Old domain to New domain (htaccess redirect)

Create a .htaccess file with the below code, it will ensure that all your directories and pages of your old domain will get correctly redirected to your new domain.
The .htaccess file needs to be placed in the root directory of your old website (i.e the same directory where your index file is placed)

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
Please REPLACE www.newdomain.com in the above code with your actual domain name.

In addition to the redirect I would suggest that you contact every backlinking site to modify their backlink to point to your new website.

Note* This .htaccess method of redirection works ONLY on Linux servers having the Apache Mod-Rewrite moduled enabled.

Redirect to www (htaccess redirect)

Create a .htaccess file with the below code, it will ensure that all requests coming in to domain.com will get redirected to www.domain.com

The .htaccess file needs to be placed in the root directory of your old website (i.e the same directory where your index file is placed)

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]
Please REPLACE domain.com and www.newdomain.com with your actual domain name.

Note* This .htaccess method of redirection works ONLY on Linux servers having the Apache Mod-Rewrite moduled enabled.

Share this article