smeehan
By:
smeehan

How To Set Up Mod_Rewrite?

December 2, 2016 675 views
Apache Ubuntu 16.04

Hi

I've followed the How To Set Up Mod_Rewrite guide.

  • I've enabled mod_rewite
  • Used sudo a2enmod rewrite to test it's been enabled. Terminal outputs: Module rewrite already enabled
  • All my website files are in /var/www/html

The guide says,

To allow the .htaccess file to override standard website configs, start by opening up the configuration file.

I ran this command sudo nano /etc/apache2/sites-available/default and the file was empty

So I copied and pasted the example

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

and ran sudo service apache2 restart to restart the apache server.

I'm using a .htaccess file I've used for other websites, so unless there's anything specific I need to include for Digital Ocean, it should work...

The .htaccess is inside the html folder located at /var/www/html

I've cleared the cache, restarted the browser and the mod_rewrite isn't working?

I then noticed the example code above was pointing at /var/www/ so I changed it to /var/www/html

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

Cleared the cache, restarted the browser and the mod_rewrite still isn't working?

Any ideas?

I'm very impressed with the speed of Digital Ocean Hosting! Would like to get this fixed :)

2 Answers

I ran this command sudo nano /etc/apache2/sites-available/default and the file was empty

I think this is where the issue is. The filename listed is the default configuration file but if your site's configuration file has a different name you need to ensure that the AllowOverride All setting is there. I would check if you have another configuration file in that directory. If you do, delete the default file you created and instead edit the existing file. Once done, restart apache with service apache2 restart and the rewrite settings in your .htaccess file should take effect.

Hi @ryanpq thanks for taking the time to reply.

There are three files in /etc/apache2/sites-available/

  • 000-default.conf
  • default
  • default-ssl.conf

I followed the guide and entered this command:

sudo nano /etc/apache2/sites-available/default

The file was empty. At no point during the installation of apache did I create an alternative config file.

If I delete the default file and run:

service apache2 restart

Will the default file be recreated?

Not sure where to go from here.

by Etel Sverdlov
This tutorial goes over Mod_Rewrite, an apache module that allows you to manipulate URLS to provide shorter or more relevant ones. This tutorial will go over Activating Mod_Rewrite, Creating and Using the required .htaccess page, and setting up the URL rewrites.
Have another answer? Share your knowledge.