Modify Your .htaccess File to Eliminate Query Strings and Make Pretty URLs

I developed a website for a client that would display different photos based on a query string in the URL.  An example of this type of URL would be “http://mysite.com/album.php?photo=ab12345”.

But, that is an ugly URL. Not only that, some people claim its not ideal for SEO.  Much better would be “http://mysite.com/ab12345”.  Here’s how to do it!  Note that you need to be running on an Apache server for this to work (that is the most common kind), as opposed to a Windows server.

The .htaccess File

The key is the .htaccess file in the root directory of your site.  If it’s not there, you can simply create it with any text editor and upload it via FTP.

This file contains a “rewrite rule”, which uses a regular expression to look at the part of the URL past the “.com”.  Here is the RewriteRule that enables the example URL above to work:

This looks pretty cryptic, I’ll try to generally explain what it does.  The first part after “RewriteRule” is the actual regular expression, which basically says, take everything after the “.com” (or .org or whatever your domain name is) and store it as parameter #1.  The second part determines what to turn the URL into.  It adds “album.php?photo=” to the URL and puts whatever was past the URL (parameter #1, specified by “$1”) after that.  The [L] means stop there; we’re done.

That is the core of what we need in the .htaccess file, but that is not all.  Here is the complete file to turn http://yoursite.com/ab12345 into http://yoursite.com/album.php?photo=ab12345, so that your site can read the URL:

Let me explain this file a bit.  The first line “turns on” the Apache mod_rewrite engine which actually performs this magic.

The second line tells the engine what the base URL of your site is.  If your site is in a subdirectory, and you want the .htaccess file in that subdirectory also, then put the name of that directory here, as in:

The next two lines tell the engine to not mess with URLs with valid file and directory names.  This is necessary to prevent a “loop” where the engine modifies the URL, then modifies it again, leading to strange results.

More Cool Things You Can Do with .htaccesss

Let’s you have a blog where your URLs are in this format: http://yoursite.com/links.html and you’d rather have http://yoursite.com/links.  The latter is more user friendly, and will stay the same if you later decide to change the HTML file to a PHP file or whatever.  Your users shouldn’t have to worry about what type of file it is!

Check out this article for how to do that and other .htaccess tricks.  Just add the RewriteRule to the file above in place of the RewriteRule there.

If you are running WordPress, this is a great explanation of how the WordPress .htacess file works.

For a more complete example including PHP code, check out this article.

Hope this helps! Please share your questions, corrections, or experiences below! – Brian

Leave a Comment or Ask a Question