Bobcares

Rewrite URLs with mod_rewrite in Apache with ease

by | Jan 13, 2021

It is possible to rewrite URLs with mod_rewrite module in Apache using .htaccess file.

Here at Bobcares, we have seen several such Apache related queries as part of our Server Management Services for web hosts and online service providers.

Today we’ll see how to rewrite URLs with mod_rewrite in Apache

 

How to rewrite URLs with mod_rewrite in Apache

Now let’s take a look at how our Support Engineers rewrite URLs with mod_rewrite.

 

1. Enabling mod_rewrite in Apache

First, we need to activate the mod_rewrite so that Apache will understand the rewrite rules. Normally, it will be already installed. But will be disabled on a default Apache installation. So we run the below command to enable it.

$ sudo a2enmod rewrite

As a result, this will activate the module or alert that the module is already enabled. Then we restart the Apache so that the changes take effect.

$ sudo systemctl restart apache2

 

2. Setting Up .htaccess

We can modify the rewrite rules without accessing server configuration files by using the .htaccess file.

Before we start using the .htaccess file, we set up a few and secure a few more settings.

By default, Apache prohibits using a .htaccess file to apply rewrite rules. So first we allow changes to the file. For that, we open the default Apache configuration file using the nano text editor.

$ sudo nano /etc/apache2/sites-available/000-default.conf

Inside that file, we will find a <VirtualHost *:80> block starting on the first line. Within that block, add the following new block so your configuration file looks like the following. Make sure that all blocks are properly indented.

<VirtualHost *:80>
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

. . .
</VirtualHost>

We save the file and close it. In order to put these changes into effect, we restart Apache.

$ sudo systemctl restart apache2

Now, we create a .htaccess file in the webroot.

$ sudo nano /var/www/html/.htaccess

Then we add this line at the top of the new file to activate the rewrite engine.

RewriteEngine on

Finally, we save the file and exit.

 

3. Configuring Rewrite URLs with mod_rewrite

Here, we will set up a basic URL rewrite that converts URLs into actual paths to pages. Specifically, we will allow users to access http://your_server_ip/about, but display a page called about.html.

First, we create a file named about.html in the webroot.

$ sudo nano /var/www/html/about.html

Next, we copy the following HTML code into the file, then we save and close it.

<html>
<head>
<title>About Us</title>
</head>
<body>
<h1>About Us</h1>
</body>
</html>

We can access this page at http://your_server_ip/about.html. But if we try to access http://your_server_ip/about, we will see a 404 Not Found error. To access the page using /about instead, we’ll create a rewrite rule.

Let’s create our URL rewrite rule. For that, we open the .htaccess file.

$ sudo nano /var/www/html/.htaccess

After the first line (containing ‘RewriteEngine on’), we add the below line.

RewriteRule ^about$ about.html [NC]

In this case, ^about$ is the pattern, about.html is the substitution, and [NC] is a flag.

  • ^ indicates the start of the URL after your_server_ip/.
  • $ indicates the end of the URL.
  • about matches the string “about”.
  • about.html is the actual file that the user accesses.
  • [NC] is a flag that makes the rule case insensitive.

We can now access http://your_server_ip/about in the browser. In fact, with the rule shown above, the following URLs will point to about.html:

  • http://your_server_ip/about, because of the rule definition.
  • http://your_server_ip/About, because the rule is case insensitive.
  • http://your_server_ip/about.html, because the original proper filename will always work.

However, the following will not work:

  • http://your_server_ip/about/, because the rule explicitly states that there may be nothing after about since the $ character appears after about.
  • http://your_server_ip/contact, because it won’t match the about string in the rule.

[Need any further assistance with Apache queries? – We are here to help you.]

 

Conclusion

In short, mod_rewrite helps in creating human-readable URLs. Today, we saw how to rewrite URLs with mod_rewrite in Apache

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.