Ratings, reviews, plans and features to help you find the right web hosting provider for your site.

Introduction to mod_rewrite and some basic examples

Web Hosting Articles » A simple guide to .htaccess » Introduction to mod_rewrite and some basic examples



ModRewrite is a powerful feature of the Apache web server. It provides an easy way to modify/manipulate URLs. As complicated as it sounds a regular webmaster can benefit from this feature in many way. I will list here the ones I consider most useful for the regular webmaster.

Create easy to remember URLs or also known as COOL URIs, other call them Search Engine Friendly URLs. For example you have some complicated site driven by some server-side scripting language: PHP, Perl, etc. In general its URLs will look like

http://www.example.com/view.php?cat=articles&a=10&page=20&lang=en

It is not easy to remember such URL.

Using ModRewrite you can make the URL look like:

http://www.example.com/en/articles/10-2

Here is the code:

RewriteEngine On
RewriteRule    ^([a-z]*)/([a-z]*)/([1-9]+)(-[1-9]+)? $  http://www.example.com/view.php?cat=$2&a=$3&page=$4&lang=$1    [L]

What the code does? It tries to match the requested URL against a regular expression string.
In case it finds a match it performs an internal rewrite. The user will never see the long URL in his browser.

Change enviromental variables
An example can be found here

The official ModRewrite documentation can be found at:
http://httpd.apache.org/docs/1.3/misc/rewriteguide.html

In the next several articles you will find some commonly used ModRewrite rules for performing everyday tasks.


  1. How to block users from accessing your site based on their IP address
  2. How to prevent or allow directory listing?
  3. How to change the error documents – 404 Page Not Found, etc
  4. Using .htaccess for password protecting your folders
  5. Using .htaccess to block referrer spam
  6. Disable Hot-Linking of images and other files
  7. Redirect URLs using .htaccess
  8. Introduction to mod_rewrite and some basic examples
  9. Force SSL/https using .htaccess and mod_rewrite
  10. 301 Permanent redirects for parked domain names
  11. Enable CGI, SSI with .htaccess
  12. How to add Mime-Types using .htaccess
  13. Change default directory page
  14. Block Bad robots, spiders, crawlers and harvesters
  15. Make PHP to work in your HTML files with .htacess
  16. Change PHP variables using .htaccess
  17. HTTP Authentication with PHP running as CGI/SuExec
  18. Force www vs non-www to avoid duplicate content on Google
  19. Duplicate content fix index.html vs / (slash only)

Comments 5 >>

Watch Avatar the last airbender Said,
Jun 07, 2010 @ 11:00

I think you made a mistake here:
http://www.example.com/en/articles/10-2

Should be:
http://www.example.com/en/articles/10/2

Am I correct? I am actually confuse. Really hope to hear your reply =)
.Net Applications Said,
Oct 25, 2010 @ 04:17

Thanks for such a nice guidance, Is there possibility to add mime-type in Windows SERVER for .htaccess support purpose ? Our some site are moved Linux hosting to Windows hosting and all are depending on Rewrite Mod.
shyamsundar bhat Said,
Nov 20, 2010 @ 22:11

Yes, in Windows, hosting provider do provide a tool called IIS Administrator Console and you can just add mime types for any type of files example: .flv, .mp4 etc. Also, you can add following code to ASP page:
its for xml.

You can even create httpd.ini file and add mime type codes for IIS and get it worked.

Anyway, I support Unix/PHP combination a lot!
www.CrazzyDeveloper.Co.CC :)
Chris Bridgett Said,
May 12, 2011 @ 08:24

Been tearing my hair out over this all morning... I had a bit of an epiphany after reading:

RewriteRule ^([a-z]*)/([a-z]*)/([1-9]+)(-[1-9]+)? $ http://www.example.com/view.php?cat=$2&a=$3&page=$4&lang=$1 [L]

...and understand exactly what I needed to do and it's working great.

:D

Many thanks.

@Watch Avatar the last airbender - No, if there was a dash inbetween the last two parts (in brackets) of the rewriting statement it would display as he intended, afaik.
Dunga Said,
Jan 23, 2012 @ 11:49

I am hoping i can get some help.

We migrated our site recently from an .asp site to Joomla.

So from url like :
http://site.com/info/facts/psychosis.asp

we now have :

http://site.com/info/facts/psychosis

Instead of writing a redirect for every url, I would want through htaccess, remove .asp from any url since some people have bookmarked the old urls.

Is this possible through htaccess?

Thanks in advance
Your comments on this article

(required)

(required but never displayed)



security code



Previous: Redirect URLs using .htaccess Next: Force SSL/https using .htaccess and mod_rewrite