How to Enable mod_rewrite in .htaccess File

mod_rewrite is an Apache module used to rewrite URLs dynamically, commonly used to create user-friendly URLs, redirect traffic, or manage different URLs for search engine optimization (SEO). If you’re working with an Apache web server, enabling mod_rewrite in your .htaccess file is a common task.

In this article, I’ll walk you through the steps to enable mod_rewrite and make the necessary changes to your .htaccess file.

What is mod_rewrite?

mod_rewrite is an Apache module that allows you to modify URLs before the server processes them.

This is very useful for:

  • Redirecting old URLs to new ones.
  • Removing query parameters (e.g., from example.com?page=about to example.com/about).
  • Making URLs cleaner and more user-friendly.
  • Helping with SEO by using readable URLs.

Step 1: Check mod_rewrite in Linux

First, you need to verify that Apache is installed on your system and that mod_rewrite is enabled.

apache2 -v   [On Debian, Ubuntu and Mint]
httpd -v     [On RHEL/CentOS/Fedora and Rocky/AlmaLinux]
Check Apache Version
Check Apache Version

If Apache is installed, you’ll see the version information. If not, you need to install it using the following appropriate command for your specific Linux distribution.

sudo apt install apache2      [On Debian, Ubuntu and Mint]
sudo yum install httpd        [On RHEL/CentOS/Fedora and Rocky/AlmaLinux]

Once Apache is installed, you need to enable mod_rewrite in Debian-based distributions and restart Apache for the changes to take effect.

sudo a2enmod rewrite
sudo systemctl restart apache2

In RHEL-based distributions, mod_rewrite is usually enabled by default, you just need to restart Apache to activate it.

sudo systemctl restart httpd

Step 2: Enable mod_rewrite Rules in .htaccess File

To enable mod_rewrite rules in your .htaccess file, you need to make sure Apache is configured to allow .htaccess overrides.

This is done by modifying your Apache configuration file.

Enable mod_rewrite in Debian/Ubuntu

Open the /etc/apache2/apache2.conf file.

sudo nano /etc/apache2/apache2.conf

Search for the section that looks like this:

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

Change AllowOverride None to AllowOverride All.

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

Save and close the file, then restart Apache.

sudo systemctl restart apache2

Enable mod_rewrite in RedHat/CentOS

Open the /etc/httpd/conf/httpd.conf file.

sudo nano /etc/httpd/conf/httpd.conf

Look for the section with:

<Directory "/var/www/html">
    AllowOverride None
</Directory>

Change AllowOverride None to AllowOverride All.

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

Save the file and restart Apache.

sudo systemctl restart httpd

Step 3: Create or Edit the .htaccess File

Now that mod_rewrite is enabled and Apache is configured to use .htaccess, it’s time to create or edit .htaccess file under your website’s root directory (/var/www/html/).

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

Add your mod_rewrite rules. For example, here is a common rewrite rule that redirects all traffic to the index.php file:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/\ [L]
</IfModule>

Save and close the file.

Step 4: Test Your Configuration

After making all these changes, you need to test your mod_rewrite configuration to make sure it’s working.

  • Open your browser and try accessing a URL that should be rewritten. For example, if your rewrite rule directs everything to index.php, try visiting http://yourdomain.com/test.
  • If everything is set up correctly, the request should be handled by index.php, even though you’re requesting /test.

If it’s not working, check your Apache error logs for clues:

sudo tail -f /var/log/apache2/error.log
Or
sudo tail -f /var/log/httpd/error.log

Common Issues

  • 500 Internal Server Error: This can happen if there’s a syntax error in your .htaccess file. Make sure the file is properly formatted.
  • RewriteEngine Not Working: Ensure that mod_rewrite is enabled and that Apache allows .htaccess overrides (with AllowOverride All).
Conclusion

Enabling mod_rewrite in your .htaccess file is essential for managing URL redirects, creating user-friendly links, and improving SEO. By following the steps above, you can easily enable and configure mod_rewrite on your Apache server.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Ravi Saive
I am an experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.