Enable Rewrite module on Apache2

Rewrite module is so important for modern web application, Basicly recently i was setup an wordpress website and i created a virtualhost for the same, I did not enable the rewrite the module due to this misconfiguration whenever the try new post i was getting invalid json error, So in this post i will show you how to enable the apache rewrite the module.

In this post, We will enable the rewrite module on Apache web server on ubuntu Server.

Step 1: Enable mod_rewrite

You need to enable the rewrite module using given command on ubuntu’s terminal.

sudo a2enmod rewrite

After that you need to restart apache2 service, Execute the given command.

sudo systemctl restart apache2.service

Step 2: Configure .htaccess file

On the Apache configuration file, you can directly configure URL rewrite rules. However, it is recommended that the guidelines remain in the “.htaccess” file on each website. The “.htaccess” file, which is created by default when the applications are installed, is used by the majority of content management systems.

Since “.htaccess” files are not supported by Apache by default, you will need to modify the virtual host file configuration for each website by adding the following code:ou can set up URL rewrite rules directly on Apache’s configuration file. However, it is advisable to keep the rules in ‘.htaccess’ file on each website. Most content management systems rely on the ‘.htaccess’ file and it is created by default once you install the applications.

By default, Apache does not allow the use of ‘.htaccess’ file so you will need to edit the configuration of each website’s virtual host file by adding the following code:

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

Add the above configuration in your every virtualhost and then restart the apache2 service.

Here you find the example virtualhost configuration.

<VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    <Directory /var/www/example.com/public_html>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

Do not forget the test apache configuration file before restart the apache2 service.

apachectl configtest

This is command show you apache2 configuration is valid and now we are good to restart the apache2 service, Use the follwing command for the same.

sudo systemctl restart apache2.service

Conclusion

We have successfully installed apache rewrite module on ubuntu 22.04 LTS, Kindly report in case you are facing defeculties with follwing details.

  • OS name
  • OS version
  • Package name
  • Logs – Error / Warning / failed

Get in touch with our new devops tools based website- https://www.devopstricks.in/

Enable Rewrite module on Apache2

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top