Let’s say you have all your pages in flat files in the same directory. In this example the files have .php extensions but they could be any kind of files. For instance:
lorem.php
lorem-ipsum.php
lorem-ipsum-dolor.php
These files end up looking like this in your browsers address bar:
http://www.domain.com/lorem.php
http://www.domain.com/lorem-ipsum.php
http://www.domain.com/lorem-ipsum-dolor.php
Let’s take these file names and turn them in to directories, this makes URI guessing easier (no file extensions) and some would say it would look better. These files showing up like directories would look like this:
http://www.domain.com/lorem/
http://www.domain.com/lorem/ipsum/
http://www.domain.com/lorem/ipsum/dolor/
If you would do this by hand it would mean making directories by hand and placing the files in those directories. Depending of the amount of pages in your website this could mean a lot of work.
Rewriting to directories
Luckily there’s and easy method to replace URI’s with mod_rewrite. The following lines in your .htaccess document will do the trick:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^([0-9A-Za-z]*)/$ http://www.domain.com/$1.php
RewriteRule ^([0-9A-Za-z]*)/([0-9A-Za-z]*)/$ http://www.domain.com/$1-$2.php
RewriteRule ^([0-9A-Za-z]*)/([0-9A-Za-z]*)/([0-9A-Za-z]*)/$ http://www.domain.com/$1-$2-$3.php [L]
</IfModule>
Make sure to change the .php extension in this code to the extension of your choice. You could also use multiple rewrite rules for multiple extensions.
Maximum directory depth
This rewrite takes up to three words per file name into account. Longer file names such as lorem-ipsum-dolor-sit.php would be rendered without any rewriting. A fourth (or more) directory rewrite could be handled by adding extra lines for each word/directory:
RewriteRule ^([0-9A-Za-z]*)/([0-9A-Za-z]*)/([0-9A-Za-z]*)/([0-9A-Za-z]*)/$ http://www.domain.com
SEO Drawbacks?
Some SEO’s would argue that file names carry more weight (in search engine algorithms) then directory’s. Other SEO’s claim that the directory carries more weight, especially without the use of a file name. Both arguments sound valid but there’s no real way to say which is true.
However, it’s a good idea too use a maximum depth of three directory’s so you don’t spread the keyword focus over too many words.
2 comments sofar
Roberthttp://dicabrio.com August 18th, 2009
Why should you do this in the first place? I recall that more directory depths give more clutter to your pages (for search algorithms) right?
Why not stick to the /lorem-ipsum.php?
Damien van Holtenhttp://www.reaact.net August 20th, 2009
The clutter is minimal when we’re talking about a depth of 2 or 3 directories. Especially when no file name is used.
The choice to do this is mainly aesthetics.
Leave a comment