Apache's mod_rewrite provides a powerful method for manipulating URLs on the server side, converting them into clean and user-friendly formats. This guide walks you through understanding mod_rewrite, its benefits, and how to configure it on a VPS running Apache.
Background
When a URL is entered, it undergoes scrutiny against a set of predefined rules. These rules identify specific patterns or keywords within the URL. If a designated keyword is detected and the rule aligns, it triggers the replacement of the identified portion with a predetermined string – thereby generating a new URL.
mod_rewrite proves invaluable as it empowers users to transform URLs into clean and user-friendly formats. This is particularly advantageous for end-users without technical expertise, as the simplified URLs are easily comprehensible and search engine-friendly.
To illustrate the concept of a clean URL, consider these examples:
- URL1:
http://modrewriteexample.com/client.php?id=A786#234QA - URL2:
http://modrewriteexample.com/client/=A786#234QA/ - URL3:
http://modrewriteexample.com/client/Martha/
Among these three URLs, the third one stands out as significantly more readable and comprehensible to the end user. URL 3 qualifies as a clean URL in this context.
Prerequisites
Before proceeding, ensure SSH access to your VPS.
How to Set Up mod_rewrite
-
Step 1: Install Apache
For this demonstration on Ubuntu 18.04, use the built-in package installer, apt-get. Begin by updating it:
sudo apt-get updateThen proceed with the installation of Apache2:
sudo apt-get install apache2 -
Step 2: Enable mod_rewrite
Enable mod_rewrite with the following command:
sudo a2enmod rewriteThis command activates the rewrite mode, or informs you if it's already in use. Subsequently, restart Apache:
sudo service apache2 restart -
Step 3: Create the .htaccess File
To establish pre-defined URL rewrite rules, utilize the
.htaccessfile. Users can manually write rules in this file. Since the server references this file, any errors could result in a server error. The.htaccessfile should be created at the root for testing the rewrite functionality.Start by running the following command:
sudo nano /var/www/html/.htaccessThis command either creates the
.htaccessfile if absent or opens it if it already exists. Save and exit in nano by pressingCTRL+Oto save andCTRL+Xto exit.Next, open the
000-default.conffile located in the/etc/apache2/sites-enabled/directory:sudo nano /etc/apache2/sites-enabled/000-default.confWithin this file, insert the following block after the
string:Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all Save the file similar to the
.htaccess. Restart Apache, as mentioned in step two, for the changes to take effect. -
Step 4: Create a URL Rewrite Rule
URL rewriting involves selecting a clean URL and transforming it into actual paths that lead to code. It comprises the following components:
- A pre-defined rewrite rule
- A pattern serving as a matching reference with the entered URL
- Rewriting lines specifying the path to be called by the server at that point
Let's create a rewrite rule to redirect a user to the
About_us.htmlpage if the requested URL is http://ip/Aboutus. For successful execution, ensure the rewrite engine is activated. Open the.htaccessfile and add the following syntax:RewriteEngine on RewriteRule ^About_us$ About_us.html [NC]Breaking down the syntax:
About_usis the pattern redirected toAbout_us.htmlupon matchingNCis a flag for case insensitivity^indicates matching immediately after the IP address
Congratulations! You have successfully created a mod_rewrite rule!
Conclusion
Easy, clean, and user-friendly URLs are crucial for a successful website. Incorporating keywords into URLs is essential for memorability and SEO. With mod_rewrite, you can create numerous rewrite rules to enhance your website's URL structure.