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.

mod-rewrite-Rule-Examples-in-Apache

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

http://localhost:80/student/studentdetails.php?id=1001

 
http://localhost:80/student/studentdetails.php?id=1001

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

 
http://localhost:80/student/studentdetails/1001

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.
sudo a2enmod rewrite

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

  • Restart the apache server with below commands
 sudo systemctl restart apache2 

If above won’t work, try below one

 sudo service restart apache2 
  • Next, we need to edit httpd.config file to accept our .htaccess file  to affect the changes of rewrite rules
<Directory /var/www/html> 
     Options Indexes FollowSymLinks MultiViews 
     AllowOverride All 
     Require all granted 
</Directory>
  • 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

RewriteEngine on

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

RewriteRule ^student/studentdetails/(.*) studentdetails.php?id=1$ [NC][QSA]
  • 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

RewriteRule ^student/studentdetails/(.*)/(.*) studentdetails.php?id=1$&amp;mangeId=$2 [NC][QSA]

For my above requirement below be the rewrite rules

RewriteEngine on 
RewriteRule ^student/studentdetails/(.*) studentdetails.php?id=1$ [NC][QSA]

Thanks,

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 *