Learn to Set Up mod-rewrite in Simple Steps

The Essential Guide to Setting Up Mod-Rewrite in Simple Steps

Apache's mod_rewrite provides a powerful and straightforward method for manipulating URLs on the server side. In essence, mod_rewrite facilitates the conversion of URLs into clean and user-friendly formats for websites. True to its name, it excels at rewriting URLs, offering an excellent solution for enhancing the clarity of your website's URLs. This tutorial guides you through understanding mod_rewrite, its applications, and the steps to configure it on a VPS running Apache!

How Apache mod_rewrite Operates

Upon entry of a URL, it undergoes scrutiny against a set of predefined rules. These rules are crafted to identify specific patterns or keywords within the URL. If a designated keyword is detected in the URL, and the rule aligns, it triggers the replacement of the identified portion with a predetermined string – thereby generating a new URL.

Why mod_rewrite is Beneficial

mod_rewrite proves invaluable as it empowers users to transform URLs into clean and user-friendly formats, offering a major advantage. This is particularly advantageous for end-users without technical expertise, as the simplified URLs are easily comprehensible.

Such URLs are not only user-friendly but also search engine-friendly. Search engines can swiftly recognize and index these clean URLs. To illustrate, consider the following example to understand the concept of a clean URL:

1. URL1: http://modrewriteexample.com/client.php?id=A786#234QA
2. URL2: http://modrewriteexample.com/client/=A786#234QA/
3. URL3: http://modrewriteexample.com/client/Martha/

Enhancing Readability with Clean URLs, among the three URLs provided, the third one stands out as significantly more readable and comprehensible to the end user compared to the first and second. Therefore, URL 3 qualifies as a clean URL in this context.

Configuring mod_rewrite on a Linux VPS

Before proceeding, ensure SSH access to your VPS. Let's initiate the setup:

1. Install Apache

For this demonstration on Ubuntu 18.04, leverage the built-in package installer, apt-get. Begin by updating it using the following command:

sudo apt-get update

Feel free to proceed with the installation of Apache2.

Execute the following command for installation:

sudo apt-get install apache2

2. Enable mod_rewrite

Now, proceed to enable mod_rewrite with the following command:

sudo a2enmod rewrite

This command activates the rewrite mode, or it informs you if it's already in use. Subsequently, restart Apache using the command:

sudo service apache2 restart

3. Create the .htaccess File

To establish pre-defined URL rewrite rules, utilize the .htaccess file. Users can manually write rules in this file. Since the server references this file, any errors could result in a server error. The .htaccess file should be created at the root for testing the rewrite functionality.

Start by running the following command:

sudo nano /var/www/html/.htaccess

This command either creates the .htaccess file if absent or opens it if it already exists. Save and exit in nano by pressing CTRL+O to save and CTRL+X to exit.

Next, open the 000-default.conf file located in the /etc/apache2/sites-enabled/ directory with the following command:

sudo nano /etc/apache2/sites-enabled/000-default.conf

Within 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.

4. URL Rewrite

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.

Now, let's create a rewrite rule to redirect a user to the About_us.html page if the requested URL is http://ip/Aboutus. For successful execution, ensure the rewrite engine is activated. Start the .htaccess file with the command:

Then, add the following syntax:

ReWriteRule ^About_us$ Aboutus.html [NC]

Breaking down the syntax:

  • About_us is the pattern redirected to About_us.html upon matching.

  • NC is a flag for case insensitivity.

  • ^ indicates matching immediately after the IP address.

Combined in the .htaccess file, it looks like this:

ReWriteEngine on

ReWriteRule ^About_us$ Abouts_us.html [NC]

Congratulations! You have successfully created a mod_rewrite rule!

Wrap Up

This demonstrates that easy, clean, and user-friendly URLs are crucial for a successful website. Incorporating keywords into URLs is essential for memorability and SEO. Numerous guidelines can be generated and documented.

We trust you will use mod_rewrite for beneficial purposes.

 

 


Was this article helpful?

mood_bad Dislike 0
mood Like 0
visibility Views: 164