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 it’s 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 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 http://yoursite.com/ab12345 into http://yoursite.com/album.php?photo=ab12345, 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: 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!
For a more complete example including PHP code, check out this article.
Hope this helps! Please share your questions, corrections, or experiences below! – Brian

I am a freelance web developer and consultant based in Santa Monica, CA. I’ve been designing websites using WordPress and from scratch using HTML, CSS, PHP, and JavaScript since 2010. I create websites and web applications for businesses, nonprofits, and other organizations. I have a degree in Electrical Engineering (BSEE) from California Institute of Technology and a degree in Engineering Management (MSEM) from Stanford University.
Discover more from Web Developer Tips and Tricks
Subscribe to get the latest posts sent to your email.
Please Leave a Question or Comment
this works but there is a cyber security risk i am facing and i am not sure how to sort it out.
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.
i was in a search of this blog and finally found it, i am a developer and i was looking for this element.