Enable mod_rewrite on Apache

Posted
Comments 0

Learn how to enable the mod_rewrite module on Apache web server.

Today I’ll show you how to enable the Apache mod_rewrite module so you can rewrite URLs using .htaccess files. This is useful for redirecting old content to new, if you change web hosting services, or even a new domain name. I’ll give a few examples too.

Contents

Prerequisites

  • You will need root access or a user with sudo privileges

Edit Virtual Hosts

Depending on your set up, the configuration file will be in different location, and possibly a different name. I will give examples of what you’re looking for:

  • For an Apache Virtual Hosts default site on port 80: sudo nano /etc/apache2/sites-available/000-default.conf
  • For an Apache Virtual Host default site on port 443: sudo nano /etc/apache2/sites-available/default-ssl.conf
  • For an Apache Virtual Host setup for a domain: sudo nano /etc/apache2/sites-available/mywebsite.com.conf

At the top of the file, just after <VirtualHost *:80> or <VirtualHost *:443>, add the following code:


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

Then save ctrl + o, ENTER and exit nano ctrl + x.

The directive that enables mod_rewrite to function is AllowOverride All which by default is set to AllowOverride None.

Enable mod_rewrite and Restart

Next, we enable to mod_rewrite module:

sudo a2enmod rewrite

The restart Apache:

sudo systemctl restart apache2

Some mod_rewrite Examples

We place any mod_rewrite rules inside an .htaccess file and upload to the folder you want to apply it too (usually the root folder public_html).

Before we start writing rules, be sure to enclose them inside a <IfModule mod_rewrite.c> to stop any errors:


<IfModule mod_rewrite.c>
	#directives go here
</IfModule>

Let’s redirect an old page to a new page:


<IfModule mod_rewrite.c>
	RewriteEngine On
	RewriteRule ^old_page.html$ new_page.html [NC]
</IfModule>

To redirect non-www to www (replace with your domain name):


<IfModule mod_rewrite.c>
	RewriteEngine On
	RewriteCond %{HTTP_HOST} ^ricmedia.com [NC]
	RewriteRule ^(.*)$ https://www.ricmedia.com/$1 [L,R=301]
</IfModule>

To redirect HTTP to HTTPS:


<IfModule mod_rewrite.c>
	RewriteEngine On
	RewriteCond %{HTTPS} off
	RewriteRule ^(.*)$ https://www.ricmedia.com/$1 [L,R=301]
</IfModule>

Restrict what file types that can be served:


<IfModule mod_rewrite.c>
	RewriteEngine on
	RewriteRule .*\.(jpg|jpeg|gif|png|bmp|exe|swf)$ - [F,NC]
</IfModule>

Redirect all URI/L requests to a new domain name. To explain, this will redirect https://www.olddomain.com/article555/index.html to https://www.newdomain.com/article555/index.html without having to write a rewrite line for each page:


<IfModule mod_rewrite.c>
	RewriteEngine On
	RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
	RewriteCond %{HTTP_HOST} ^www.olddomain.com$
	RewriteRule (.*)$ https://www.newdomain.com/$1 [R=301,L]
</IfModule>

Conclusion

I hope you’ve enjoyed this article and learned some handy mod_rewrite rules. If you ran into any trouble, make a comment or contact me.

Newsletter Signup







Privacy Policy

See Also

Further Reading

Author
Categories Apache

Comments

There are currently no comments on this article.

Comment

Enter your comment below. Fields marked * are required. You must preview your comment before submitting it.





PLEASE NOTE: You must preview a comment before submitting

Comments use Textile formatting

Update cookies preferences