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 “”.

But, that is an ugly URL. Not only that, some people claim it’s not ideal for SEO.  Much better would be “”.  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 an NGINX or 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:

RewriteRule ^([^/]*)$ album.php?photo=$1 [L]

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 into, so that your site can read the URL:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ album.php?photo=$1 [L]

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:

RewriteBase /blog/

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: and you’d rather have  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!

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

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


Please Leave a Question or Comment

Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
4 years ago

this works but there is a cyber security risk i am facing and i am not sure how to sort it out.

4 years ago

I’ve recently made some changes to my site, but I want to redirect everyone, especially spiders, who access the old page to go to the new page.

salena james
5 years ago

i was in a search of this blog and finally found it, i am a developer and i was looking for this element.