How to Enable Apache mod_rewrite Module in Ubuntu

In apache on 1~3 minutes
How to Enable Apache mod_rewrite Module in Ubuntu

Apache mod_rewrite module helps you to manipulate URLs. For example in WordPress, instead of accessing a blog post by https://example.com/?p=1, you can use the mod_rewrite module to access that post with an SEO friendly pretty URL such as https://example.com/my-first-post. Also, the mod_rewrite module can be used for complex conditional URL redirections.

Activate the Apache mod_rewrite module with the following command.

sudo a2enmod rewrite

But still, it will not work as we have to override the Apache global configuration. For that, open the 000-default.conf file in /etc/apache2/sites-available/ directory.

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

You will be able to see a file similar to this (comments removed for simplicity).

<VirtualHost *:80>

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

Add the <Directory /var/www/> code block into it, now the final file should be something similar to the following example.

<VirtualHost *:80>

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

         <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
         </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

Restart the Apache server to make changes effective.

sudo service apache2 restart

Now you can use a .htaccess file to mention the necessary rewrite instructions. You can find more information about the Apache mod_rewrite module in their documentation.

Share: