How To Rewrite URLs with mod_rewrite for Apache on Debian 9


rewrite urls with modrewrite for apache on debian

Apache’s mod_rewrite module allows you to rewrite URLs easily, setup redirects as well as define conditions to rewrite URLs. An .htaccess file is its backbone, that allows you to easily define rewrite rules without even accessing server configuration. You can place this .htaccess file at the root of your site, to manage rewrites on a per-site or per-subfolder basis.

 

How To Rewrite URLs with mod_rewrite for Apache on Debian 9

Here are the steps to rewrite URLs with mod_rewrite for Apache on Debian 9

 

1. Install Apache

You can skip this step, if you have already installed Apache.

You can easily install Apache on Debian using its package manager. First, begin by updating the package manager


$ sudo apt update

This will update all the packages in your Debian system, remove outdated ones and prevent any packages from interfering with the installation. Next, install Apache.


$ sudo apt install apache2

 

2. Enable mod_rewrite

For Apache to understand your rewrite rules, you need to enable its mod_rewrite module. Although mod_rewrite is also installed during Apache installation, it is disabled by default.


$ sudo a2enmod rewrite

It will activate the module or show a message that it is already activated. To apply these changes, restart your Apache server


$ sudo systemctl restart apache2

Now that mod_rewrite is enabled, we will create an .htaccess file where you can define your URL rewrite rules.

 

3. Set up .htaccess file

.htaccess file allows you to define URL rewrite rules without modifying server configuration files. Therefore it is quite critical to your server’s security, and is preceded by a dot(.), making it a hidden file.

However, by default, Apache forbids using .htaccess to set up rewrite rules. So you will have to allow changes to this file.

Open Apache’s server configuration file using nano or your favorite text editor


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

In that file, look for a block of code starting with <VirtualHost *:80>

In that block, add the following lines,


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

Make sure, they are indented properly as shown.

Save and close the file. Test the config file


$ sudo apache2ctl configtest

 

If you don’t see any errors, restart your Apache server


$ sudo systemctl restart apache2

 

Now you can create .htaccess file in Apache’s root folder


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

 

Add the following line at the top


RewriteEngine on

 

Now you have set up a fully functional .htaccess file where you can define routing rules for your website/web application.

 

4. Set up URL Rewrites

Here we will setup a simple URL rewrite that converts pretty URLs into actual path to pages. For example, when a users try to access www.your_domain.com/about they are shown a page called about.html

Start by creating a file called about.html in Apache’s root:


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

 

Paste the following HTML code to create a simple HTMl file


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

 

You can access this page at www.your_domain.com/about.html but if you try to access www.your_domain.com/about you will see a 404 Not Found error. To access this URL, we’ll create a rewrite rule.

 

Rewrite rules have the syntax


RewriteRule pattern substitution [flags]
  • RewriteRules – Server directive for defining rewrite rules
  • Pattern – A regular expression that matches the desired string in requested URL
  • Substitution – Where the requested URL should be sent for processing
  • [Flags] – optional parameter to modify the rule. Here’s the detailed documentation about Rewrite Flags.

.htaccess can have more than one rewrite rules. However, Apache will execute them in the defined order only. When Apache gets a URL that matches one of the rewrite rules, it will remap that URL to the destination URL.

Open your .htaccess file using a text editor


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

 

After the first line, add the following rule:


RewriteRule ^about$ about.html [NC]

 

So your .htaccess file will look like:


RewriteEngine on
RewriteRule ^about$ about.html [NC]

 

In the above example, ^about$ is the pattern, about.html is the substitution, and [NC] is the flag.

  • ^ indicates the start of the URL, that is, www.your_domain.com
  • $ indicates the end of URL
  • about matches the string ‘about’ in URL
  • about.html is the file served for this request
  • NC is a flag to make the rule case insensitive

Now, you can access not only www.your_domain.com/about but also:

  • www.your_domain.com/About
  • www.your_domain.com/about.html
  • www.your_domain.com/ABOUT

 

To apply the changes, restart your Apache server

$ sudo service apache2 restart

 

Now you can rewrite URLs with mod_rewrite for Apache on Debian 9.

 

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!