web hosting reviews


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)

Leave a comment >>

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

Back to Web Hosting Articles list

Web hosting companies A-Z:
All web hosting companies


About - Terms of Use - Privacy Policy -
Web Hosting Reviews