Ugacomp

How can I enable and use mod_rewrite in Apache?

Where necessary, you may need to have access to a VPS server so you can follow how to implement the steps in this article.  You can get a cheaper VPS Server from Contabo with 4vCPU cores, 8GM RAM, and 32TB Bandwidth for less than $5.50 per month. Get this deal here now

Table of Contents

Cloud VPS S

$5.50 Monthly
  • 4 vCPU Cores | 8GB RAM

CLOUD VPS M

$15.50 Monthly
  • 6 vCPU Cores | 16GB RAM

CLOUD VPS L

$17.50 Monthly
  • 8 vCPU Cores | 24GB RAM

In the Apache web server, mod_rewrite is an extension module that provides a powerful way to manipulate URLs. It allows you to modify the URL requested by a client and serves different content without the client being aware of it. This can be helpful for various purposes such as creating user-friendly URLs, implementing URL redirection, enforcing SSL, and more.

To enable and use mod_rewrite in Apache, follow these steps:

Install Apache on the System

First, make sure you have Apache installed on your server. If it’s not installed, you need to install it. The method of installation varies based on your operating system.

Enable the mod_rewrite module

The mod_rewrite module is usually installed by default with Apache, but it might not be enabled. But you can also check if it is installed by running the following command on your system:

apachectl -M | grep rewrite

Running the above command will output the following:

mod_rewrite.c:Loaded

The above output indicates that the mod_rewrite module is loaded and enabled.

But if it’s not enabled, then you can use the a2enmod command on Debian-based systems (like Ubuntu) or the LoadModule directive on other systems.

For Debian-based systems, use the following command:

sudo a2enmod rewrite
sudo systemctl restart apache2

For other systems, edit your Apache configuration file (usually located at /etc/httpd/conf/httpd.conf or /etc/apache2/apache2.conf) and ensure the following line is present and not commented out:

LoadModule rewrite_module modules/mod_rewrite.so

Mod_rewrite & URL redirects

Mod_rewrite rules are typically placed in a file called .htaccess, which should be located in the root directory of your website. If the file doesn’t exist, you can create it. Here’s an example of how you can use mod_rewrite rules in the .htaccess file:

RewriteEngine On
RewriteRule ^old-page$ /new-page [R=301,L]

In this example, requests to http://example.com/old-page would be redirected to http://example.com/new-page with a 301 (permanent) redirect.

After enabling mod_rewrite and configuring your .htaccess file, you need to restart Apache for the changes to take effect. Use the appropriate command for your system:

sudo systemctl restart apache2

Hire us to handle what you want

Hire us through our Fiverr Profile and leave all the complicated & technical stuff to us. Here are some of the things we can do for you:

  • Website migration, troubleshooting, and maintenance.
  • Server & application deployment, scaling, troubleshooting, and maintenance
  • Deployment of Kubernetes, Docker, Cloudron, Ant Media, Apache, Nginx,  OpenVPN, cPanel, WHMCS, WordPress, and more
  • Everything you need on AWS, IBM Cloud, GCP, Azure, Oracle Cloud, Alibaba Cloud, Linode, Contabo, DigitalOcean, Ionos, Vultr, GoDaddy, HostGator, Namecheap, DreamHost, and more.
 

We will design, configure, deploy, or troubleshoot anything you want. Starting from $10, we will get your job done in the shortest time possible. Your payment is safe with Fiverr as we will only be paid once your project is completed.