How to Enable mod_rewrite in Apache on Ubuntu

How to Enable mod_rewrite in Apache on Ubuntu

How to Enable mod_rewrite in Apache on Ubuntu

Works on Ubuntu 18.04, 20.04, 22.04, 24.04+
Compatible with Apache 2.4+

Step 1: Install Apache (If Not Already)

If Apache isn't installed yet:

sudo apt update sudo apt install apache2 -y

Check Apache status:

sudo systemctl status apache2

Step 2: Enable mod_rewrite Module

Run the following command:

sudo a2enmod rewrite

Step 3: Configure Apache to Allow .htaccess Overrides

Open your Apache config file. If you're using the default site:

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

Inside the <VirtualHost *:80> block, add or edit this section:

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

This allows .htaccess files in your /var/www/html directory to work.

Step 4: Restart Apache

Apply the changes by restarting Apache:

sudo systemctl restart apache2

 Step 5: Create a Test .htaccess File (Optional)

You can test if mod_rewrite works by creating a .htaccess file:

sudo nano /var/www/html/.htaccess

Paste the following:

RewriteEngine On RewriteRule ^hello$ test.html [L]

Now, create a file:

echo "It works!" | sudo tee /var/www/html/test.html

Visit:
http://localhost/hello
You should see the contents of test.html.

Done!

mod_rewrite is now enabled and working on your Apache server! You can now use .htaccess files and pretty URLs in your web apps.

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close