Rewriting URLs with .htaccess

September 4th, 2012 Leave a comment
Like the article?
Rewriting URLs with .htaccess

Rewriting URLs is a useful tool for hiding or removing the file extension so your users do not know which programming or scripting language is being used to produce the page. This gives you an extra measure of security since a hacker or malicious person would not know how to intrude or inject code in your website, since they do not know which language you are using. In this article we will look at how to change the file extension of your requested URLs as well as rewrite the URLs to something completely different.

Another benefit of rewriting URLs is for search engines. Many search engines see the "?" in the URL and identify it as a dynamic URL, by changing the dynamic URL to a static format it becomes a more SEO friendly URL. Search engine robots will sometimes ignore these dynamic URLs and not add them to the index since the page content could change frequently.

Using our .htaccess file, we can change how certain file extensions are displayed in the URL. Here is how you would write the command in the .htaccess file:

#this changes PHP files to be HTML
AddType application/x-httpd-php .htm .html

By placing this command into your .htaccess file you will be able to use PHP files but the URL will make it appear that the files are static HTML pages. This is great if you just want to change the file extension to prevent users from knowing what scripting language you are using but if we want to change the whole URL, things get a little more complex.

There are some prerequisites that need to be setup and loaded in Apache server. The mod_rewrite module needs to be loaded and the FollowSymLinks option has to be enabled. I will walk you through turning on the mod_rewrite and FollowSymLinks in-case you haven’t done this before. If you already have this setup, you can skip down to the next section.

Enabling mod_rewrite on Apache

  1. Create a PHP info script so you can view the enabled/disabled features of your server. Do this by creating a new PHP script and put <?phpphpinfo(); ?>
  2. Save it and browse to that file on the server through the browser. What you will see a configuration file listing.
  3. Search for “mod_rewrite” using the browser’s search feature.
  4. When you have found it, check and see if it is under the “Loaded Modules” section. If so, it is already loaded and ready to go.
  5. If not, then you need to turn it on. Locate your httpd.conf file in the “conf” folder of your Apache installation.
  6. Locate the following line: "#LoadModulerewrite_module modules/" in the “httpd.conf” file.You can do this easily by searching the keyword “mod_rewrite” from find menu.
  7. Remove the hash mark (#) from the start of the line. This will uncomment the module.
  8. Restart your Apache server for the change to take effect.
  9. Check your PHP info script from the first step and make sure the module is now loaded.

Turning on FollowSymLinks

Like the previous section, if you already have this enabled you can skip this part.

FollowSymLinks is a shorthand version of the Follow Symbolic Links directive that tells your web server to follow symbolic links. You can find the FollowSymLinks directive is two places, the httpd.conf file and the .htaccess file.

  1. 1. Locate your httpd.conf file in the “conf” folder of your Apache installation.
  2. 2. Make sure you have “Options FollowSymLinks” (no quotes) under your <Directory/> section
  3. 3. If you had to add it, restart your Apache server for the change to take effect.

Rewriting URLs

As I mentioned previously, creation of more SEO friendly URLs is a good reason to use URL rewriting since it helps the search engine index your pages and dynamic pages will no longer be ignored. These rules are inserted into your .htaccess files and are written using Regular Expressions to decide which pages the rule will apply to. These rules also make use of Apache’s RewriteRule Flags that define different behaviors xmlp>

Here is an example of how to rewrite a dynamic PHP URL for HTML:

Options +FollowSymlinks
Rewrite Engine on
RewriteRule^(.*)\.htm$ $1.php [nc]

This rule is setup to catch any PHP file and rename it to an .htm file. So if http://localhost/file.php is called, the URL will be rewritten to http://localhost/file.htm. As you can see, the RewriteRule is setup using regular expressions to catch all PHP files. The [nc] flag means that it is not case sensitive.

Here is a more advanced example:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^user-([0-9]+)\.html$ user.php?id=$1

This rule removes the dynamic URL marker of “?” and rewrites the URL to a more SEO friendly .html file. Any time a URL such as http://localhost/user.php?id=10 is called, it will be changed to http://localhost/user-10.html

Performing a 301 Redirect with URL Rewriting

If you need to do a redirect rewrite there is also a lot of benefit to that. A 301 redirect is a more friendly way for to keep your search engine ranking even though you site or files have moved. This is also a nice way to create a redirect from to, which prevents your site from being split by search engine robots as well as an easier way for user’s to reach your site. You can put the following RewriteRule into your .htaccess file to create this redirect:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomain\.com
RewriteRule (.*)$1 [R=301,L]


Whether you just want a more friendly way for users to reach your site or you want to create an extra layer of security by hiding your site’s technology, performing URL rewrites is an easy task. The .htaccess file is a powerful tool, coupled with Regular Expressions which allows you to create as complex rules as you may need. Remember that you also get search engine optimization benefits and better rankings by making your dynamic pages easier to crawl by the search engine bots.


Here is an excellent site called Apache Cheat Sheet that contains useful Regular Expression syntax, RewriteRule flags, server variables and lots of other helpful information.

Help us spread the word!
  • Twitter
  • Facebook
  • LinkedIn
  • Pinterest
  • Delicious
  • DZone
  • Reddit
  • Sphinn
  • StumbleUpon
  • Google Plus
  • RSS
  • Email
  • Print
Don't miss another post! Receive updates via email!