How to Install the Mod_rewrite Module in Apache?

How to Install the Mod_rewrite Module in Apache?

Generally speaking, mod_rewrite converts clean and user-friendly URLs to websites. As you can tell by its name, it rewrites the URL. A great way to clear your website’s URLs.

How Apache mod_rewrite Works

When a user enters a URL, the URL is checked against a list of predefined rules. The rules are to search for specific phrases or keywords. If the URL contains the keyword and conforms to the rule, it will be replaced by a predetermined set of URLs.

What Makes Mod_rewrite Useful?

The major advantage of mod_rewrite is that it can convert URLs to clean URLs. This is easily understood by the end-user, who is not familiar with the technology.

These URLs are very user-friendly and search-friendly. Search engines detect these URLs faster. What do we mean by a clean URL?

As an example:

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

The result of the three URLs listed above is that the third end-user is far more readable and understandable than the first and second. According to this example, URL3 is a clean URL.

Apache mod_rewrite in Linux VPS How to install?

Please note that you must access your VPS using SSH before you begin! You can use Putty for this.

1. Install Apache

We will use Ubuntu 18.04 for this example. There is a built-in package installer – apt-get. The first update using this command:

sudo apt-get update

You can now proceed with the installation of apache2:

The command required for installation:

sudo apt-get install apache2

2. Activate mod_write

Now we’ll show you how to activate mod_rewrite.
The command to activate is:

sudo a2enmod rewrite

The above command will activate mod_rewrite or notify you if it is already in use. After that, restart Apache:

sudo service apache2 restart

3. Create your .htaccess file

URL rewrite rules must be predefined. That’s where .htaccess comes to your help. You can write all the rules to the .htaccess file. This file is used by the server. There should be no errors in this file, otherwise, you will get a server error. You can edit the rewrite rules at any time.

The .htaccess file must be created at the root to test rewrite functionality.

First, run the following command:

sudo nano /var/www/html/.htaccess

This will create the .htaccess file if it doesn’t exist yet, and open the file if it already exists. You can save and exit for now. In Nano, you can save it by clicking CTRL + O and exit with CTRL + X.

Now open 000-default.conf in  /etc/apache2/sites-enabled/. You can do this with the following command:

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

Copy and paste the following column under the file <VirtualHost *:80> in this file:

<Directory /var/www/html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow, deny
allow from all
</Directory>

Save the file as you would with .htaccess. Restart Apache as we mentioned in the second step for the above changes to take effect.

4. URL Rewriting

URL rewriting simply takes the clean URL and converts it to paths to the script. What he should have:

  • Predefined rewrite rule
  • A pattern – the presented pattern will work as a reference compatible with the URL entered by the user
  • Rewrite lines – will determine the path required by the server during operation

Now, for example, we’ll write a rewrite rule that will redirect a user to a page called Aboutus.html. The requested URL is http: // ip /Aboutus.

The rewrite engine must be active to successfully run the rewrite rule. Enter the following command at the beginning of the .htaccess file:

RewriteEngine on

The following is the rule of our example:

ReWriteRule ^About_us$ Aboutus.html [NC]

This syntax can be a bit confusing but is quite simple:

  • In the above rule, About_us is a pattern, and when it is found and mapped, it will be redirected to About.html
  • NC is the flag that determines the case-insensitive rule
  • ^ indicates that the text after the IP address is mapped

The combination of these two lines will look like this in the .htaccess file:

ReWriteEngine on
ReWriteRule ^About_us$ Aboutus.html [NC]

That’s what we do! We’ve successfully created a mod_rewrite rule!

I hope it has been a useful article.