mod_rewrite Rule Examples in Apache: The Apache module mod_rewrite is a very powerful and sophisticated module that provides a way to do URL manipulations. With this, you can do nearly all types of URL rewriting that you may need.


It is, however somewhat complex, and maybe intimidating to the beginner. There is also a tendency to treat rewrite rules as a magic incantation, using them without actually understanding what they do

When to use the Rewrite Module?

For Instance: If you have an URL like


Now you want to see .php in your URL and query parameters in URL we can change existing URL to:

With the help Of mod_rewrite Module. We can manipulate complex URLs also and send them to the respective PHP or other services.

How to Use Mod_rewrite?

  • We need to enable to mod_rewrite module in apache server
  • For enabling the rewrite module in the Linux machine, use the below command.

Above command will help you to enable rewrite Module in your apache server

  • Restart the apache server with below commands

If above won’t work, try below one

  • Next, we need to edit httpd.config file to accept our .htaccess file  to affect the changes of rewrite rules

  • Next, we need to create a .htaccess file in the root folder of your application ( we need to create .htaccess for each application separately )

Note: File name should be .htaccess only

below is the code need to be used in .htaccess file

The above line will tell the rewrite engine is enable for the application you are doing

  • Above Rule will works when URL starts with student/studentsdetails/(anything here) & it will rewrites to URL studentsdetails.php&id=“capture value from above rule” 
  • In the above rule, we wrote [NC] it means rewrite rule URL is not case sensitive.
  • [QSA] Means if you send any additional to requests then all query parameters are forward with the same Key and value to rewrite rule
  • [L] Means this will be the last rule to apply for your rewrite rules

In above rewrite rule we are capturing only one value, In your case if you have multiple values need to capture and forward then you can use N number of (.*) with respective left and right boundary

like below

For my above requirement below be the rewrite rules


Also Read: Creating SSL Certificate and Downloading from Godaddy Installing on Apache

mod_rewrite Rule Examples in Apache

Leave a Reply

Your email address will not be published. Required fields are marked *