Mod_rewrite is a powerful Apache module that provides URL manipulation capability. The sophisticated feature allows webmasters to rewrite URLs and this is a common practice in many content management systems coding like WordPress.
Mod_rewrite is famous for translating human-readable URLs in dynamic websites. This makes the URL’s look cleaner and friendly in websites.
In this guide, we are going to cover the steps of enabling mod_rewrite on your Apache Server running on an Ubuntu 18.04 VPS.
- Ubuntu 18.04 VPS
- A non-root user with sudo privileges
- Apache web server
Step 1: Enable mod_rewrite
You can enable any Apache module using the a2enmod command. So run the command below on your Ubuntu 18.04 server:
$ sudo a2enmod rewrite
In case the module is already enabled on your server, you will get an alert message.
You must restart Apache once you make any change to its configuration. To do this, type the command below on a terminal window:
$ sudo systemctl restart apache2
Your server is now ready to accept rewrite rules.
Step 2: Setup your server to accept .htaccess files
You can set up URL rewrite rules directly on Apache’s configuration file. However, it is advisable to keep the rules in ‘.htaccess’ file on each website. Most content management systems rely on the ‘.htaccess’ file and it is created by default once you install the applications.
By default, Apache does not allow the use of ‘.htaccess’ file so you will need to edit the configuration of each website’s virtual host file by adding the following code:
<Directory /var/www/html> Options Indexes FollowSymLinks MultiViews AllowOverride All Require all granted </Directory>
For instance, you can edit the default virtual hosts that ships with Apache using a nano editor by typing the command below:
$ sudo nano /etc/apache2/sites-available/000-default.conf
Then copy paste the above text just before the ‘</VirtualHost>’ closing tag. Remember to save the file by pressing CTRL + X then Y and Enter
Then, restart Apache for the changes to take effect:
$ sudo systemctl restart apache2
Step 3: Mod-rewrite syntax
The basic Apache mod_rewrite syntax has the following parts:
RewriteRule pattern substitution [flags]
- RewriteRule – the directive of our rule.
- Pattern – this is a regex (Regular Expression) that matches what the user types in a browser.
- Substitution – The actual URL path that should be called by the Apache server.
- Flag – optional parameters that modify how the rules work..
Step 4: Create a sample .htaccess file
We will now create a sample ‘.htaccess’ file at the root of the default website to test mod_rewrite. To do this type the command below
$ sudo nano /var/www/html/.htaccess
Every mod_rewrite rules must be with the commands ‘RewriteEngine on’. So you need to type this at the top of the file.
Next, we are going to rewrite a rule that redirects users to a ‘contact_us.html’ page if they request the URL http://ipaddress/contact_us
So we add the below rule:
RewriteRule ^contact_us$ contact_us.html [NC]
In the above rule, ‘contact_us’ is the pattern that should be matched and redirected to our substitution path ‘contact_us.html’. The command ‘[NC]’is a flag that tells Apache to make the rule case insensitive. ‘^’ indicates that we are matching any text after the server public IP address or domain name while ‘$’ signifies the end of the URL that we are matching.
So our complete ‘.htaccess’ a file should look like the text below:
RewriteEngine on RewriteRule ^contact_us$ contact_us.html [NC]
Save the file by pressing CTRL+ X, Y, and Enter.
Next type the command below to create the contact_us.html page:
$ sudo nano /var/www/html/contact_us.html
Then, paste the HTML text below on the file:
<html> <head> <title>Contact our website</title> </head> <body> <h1>This is a contact us page</h1> </body> </html>
Save the file by pressing CTRL+ X, Y and Enter.
Now if you visit the path http://ipaddress/contact_us on a browser, Apache should serve you with the page ‘contact_us.html’ that we created like shown below:
Meaningful URLs are very important both to humans and search engines. In fact, including keywords on the URLs instead of numbers make your website SEO-friendly. There dozens of rules that you can write on your Apache web server but the above guide was a simple tutorial for enabling mod_rewrite on your machine and making sure the module works as expected.
If you were unable to run a content management system like WordPress before, it should now work. We hope you enjoyed reading the guide and you are going to apply the steps learned on your Ubuntu 18.04 VPS.
Check out these top 3 Linux hosting services