How to Enable Apache Mod_Rewrite

How to Enable Apache Mod_Rewrite

Apache’s mod_rewrite is a powerful module used for URL redirection and updates, essential for optimizing website performance and search engine rankings. Knowing how to enable and configure mod_rewrite can transform the way your site interacts with web browsers and search engines. In this article, we provide all you need to get started with enabling Apache mod_rewrite, from installation to testing, whether you’re a seasoned web developer or just beginning to explore server-side configurations.

What is Apache Mod_Rewrite?

Apache Mod_Rewrite is a module for the Apache HTTP Server that allows for the rewriting of URL requests that come into the server. It provides a way to use rule-based rewriting logic to change the requested URL’s appearance, control the flow of requests, and redirect URLs based on specific conditions.

For example, Mod_Rewrite can be used to make URLs more user-friendly and search engine-friendly by removing file extensions, transforming long URLs into short and readable ones, or redirecting old URLs to new ones.

This functionality is highly valuable for website administrators and developers, as it offers increased flexibility in managing website navigation and enhances search engine optimization (SEO). It is often utilized to ensure consistency across a website, direct traffic appropriately, and manage changes or updates to a site’s structure.

Enabling Apache Mod_Rewrite

Here’s a general guide to enabling this module.

#1 Install Apache HTTP Server

Make sure you have Apache installed on your server. Run the following command to install Apache.

sudo apt install apache2

#2 Enable mod_rewrite Module

Depending on your operating system and Apache version, this might differ. On Ubuntu, for example, you can use the following command:

sudo a2enmod rewrite

#3 Update Apache Configuration

Next, you’ll need to allow the use of .htaccess files to control URL rewriting. This is done by editing the Apache configuration file for your site, typically found in /etc/apache2/sites-available.

Open the file for your site:

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

Find the <Directory /var/www/html> block and change the AllowOverride directive from None to All. If the line doesn’t exist, you can add it:

<Directory /var/www/html>
    AllowOverride All
</Directory>

This change allows .htaccess files in the /var/www/html directory (or the appropriate directory for your site) to include mod_rewrite rules.

#4 Restart Apache

After enabling the module, you must restart the Apache server to apply the changes.

sudo systemctl restart apache2

#5 Set Up .htaccess File

Create an .htaccess file in the directory where you want the rewrite rules to apply, with an example like.

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

#6 Write Rewrite Rules

You can now write your rewrite rules using the RewriteRule directive inside your .htaccess file or directly in your Apache configuration file.

Using .Htaccess file. If you choose to use an .htaccess file, make sure it’s located in the directory where you want the rules to apply. You can add the following lines to the .htaccess file:

RewriteEngine On
RewriteRule ^old-page\.html$ /new-page.html [R=301,L]
RewriteRule ^product/([0-9]+)$ /new-product/$1 [R=302,L]

Explanation:

  • The first rule redirects all requests for “old-page.html” to “new-page.html.”
  • The second rule redirects product URLs with a numerical ID to new URLs, keeping the ID.

Using Apache Configuration File. Alternatively, you can add the rewrite rules directly to your Apache configuration file (e.g., /etc/apache2/sites-available/000-default.conf):

<Directory /var/www/html>
    RewriteEngine On
    RewriteRule ^old-page\.html$ /new-page.html [R=301,L]
    RewriteRule ^product/([0-9]+)$ /new-product/$1 [R=302,L]
</Directory>

This approach has the same effect but can offer more control and potentially better performance, especially on high-traffic sites.

#7 Test Your Rewrite Rules

Ensure that your rewrite rules are working as expected by visiting the URLs that should be affected. Assume you’ve set up the following rewrite rules in your .htaccess file:

RewriteEngine On
RewriteRule ^old-page\.html$ /new-page.html [R=301,L]
RewriteRule ^product/([0-9]+)$ /new-product/$1 [R=302,L]

Example of Testing the Rewrite Rules.

Visithttp://yourdomain.com/old-page.html
Expected Redirecthttp://yourdomain.com/new-page.html
Test Case 1
Visithttp://yourdomain.com/product/123
Expected Redirecthttp://yourdomain.com/new-product/123
Test Case 2

Tools for Testing

  • Browser. You can directly visit the old URLs in your web browser to see if they redirect to the new URLs as expected. Check the address bar to ensure that the URL has changed.
  • Curl. You can use the command-line tool curl with the -I option to see the HTTP header and check the redirect status:
curl -I http://yourdomain.com/old-page.html

Look for a “Location” header with the new URL and a 301 or 302 status code, depending on your configuration. If any of the tests fail, consult your Apache error log (/var/log/apache2/error.log) for detailed information on what might be going wrong, and make necessary adjustments to your rewrite rules.

That’s it! you’re now enabled Apache mod_rewrite which is a straightforward process that can significantly enhance your website functionality.

Conclusion

Want to enhance your Apache experience? Gcore’s Web Application Security, Managed Logging, Virtual Instances, VPS/VDS, and Bare Metal have you covered.

  • Web Application Security enhances the security of your Apache web server by protecting it from threats such as SQL injection and cross-site scripting, ensuring a secure experience for your website’s visitors
  • Managed Logging automatically handles the logging of all events on your Apache server, making it easier to monitor its performance and troubleshoot any issues that arise
  • Virtual Instances are virtual machines with a variety of configurations and an application marketplace
  • Virtual Dedicated Servers provide an ideal environment to host your Apache web server, giving you more control and flexibility over your server’s configuration and allowing you to optimize its performance to suit your specific needs
  • Bare Metal offers the ultimate in server power and performance with an entire server dedicated to your needs alone

Secure Apache today

How to Enable Apache Mod_Rewrite

Subscribe
to our newsletter

Get the latest industry trends, exclusive insights, and Gcore
updates delivered straight to your inbox.