How to Rewrite URLs


Why Rewrite Your URLs?

Rewriting your URLs to clean URLs is helpful to search engines and your users. It is helpful to search engines because they no longer have to deal with messy parameter values. It also helps the search engine result pages (SERPs) show the relevant URL to the user's query. For instance, Google.com currently shows the URL below the title. When the user's query matches part of the URL, Google bolds that section to provide more feedback to the user. Additionally, if the URLs are long, Google cuts out the middle and shows the first and last part of the URL. With clean URLs, your keyword phrase is generally at the end of the URL, which should also benefit the user. They can now look at the URLs and understand what the page is about before visiting it. How to rewrite URLs depends on your web server.

Please note: This is a basic tutorial that is intended to teach you how to rewrite URLs to look nice and clean. It is not an advanced tutorial to cover all of the topics in rewriting URLs. A quick Google search will help those looking to learn more about advanced rewriting.

Rewriting URLs With Apache

With an Apache server, rewrite URLs is relatively easy. You should have an .htaccess file in your root directory (if not create one with a text editor and upload it). We will use an example of a website with a series of database articles and PHP files. The structure of the website will determine how we want to rewrite our URLs. Websites must have a collection of files, a collection of database records that are shown with at least one file, or a mixture of both. The following examples would be placed inside the website's .htaccess file.

Rewrite Engine

First, you need to enable the RewriteEngine to allow for URL rewrites.

RewriteEngine on

Consistent Trailing Slash

With our URL rewrites enabled, you want to make your URLs consistent with or without a trailing slash. The following code in your .htaccess file will force a trailing slash on each request.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ /$1$2/ [R=301]

Rewrite URLs for a Folder of Files

Now, we want to rewrite a collection of files that are articles in a particular folder named "files". So, we might have a real URL of www.example.com/files/some-file.php, but we want users to navigate to www.example.com/files/some-file/ and be shown the content from the previous URL. The following code would do just that if we had a file named "some-file.php" in our "files" folder.

RewriteRule ^files/([-_.a-zA-Z0-9]+)/$ /files/$1.php

Rewrite URLs for Database Records Shown in One File

We might also have a single file that retrieves a database record based on a query parameter. An example URL would be www.example.com/articles/articles.php?articleName=Some-Article. To make this URL look clean, we could use the following code:

RewriteRule ^articles/((.*)+)/$ /articles/articles.php?articleName=$1

Now, our users could navigate to the URL www.example.com/articles/Some-Article/ and they would be shown the same content that is on www.example.com/articles/articles.php?articleName=Some-Article.

Rewriting URLs With IIS

If you don't have an Apache server, you probably have a Microsoft IIS server. Unfortunately, IIS also makes the URL rewriting process much more difficult and we will not cover it in this basic tutorial. However, you can deal with Microsoft's IIS documentation if you are unwilling to switch to Apache. Microsoft has an IIS URL Rewrite Module that is designed to handle this situation. Sorry that we cannot provide a basic tutorial, but Google should retrieve excellent tutorials about IIS URL rewriting.