How To Set Up Mod_Rewrite?


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

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

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 :)

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Ya this document is outdated. The current location of the document root in CentOS 7 is now in the /etc/httpd/sites-available. And if your doing vhosting your document roots can be in the various configuration files I believe in that directory. Im very confused at this point because, some of the tutorials for CentOS 7 seem to say that the document root is /var/www/html/ and some of your vhost documentation says the document root for the vhosts are /var/www/

I followed this document “How To Set Up mod_rewrite for Apache on CentOS 7” and go to the section where it says to edit the httpd.conf file and changed the <Directory “/var/www/html”> sections AllowOverride None to AllowOverride All I then had to add the .htaccess file in my vhosts public_html folder here /var/www/ Is this the wrong location? All The code I added to my new .htaccess file is here below because all I want to do is make all requests from Apache go to and there is a site that tells how to do it ( My .htaccess code looks like this below. Is anything Im doing correct?


RewriteEngine On RewriteCond %{HTTP_HOST} ^ RewriteRule (.*)$1 [R=301,L]

Please advise. P.S. I have been using the “How To Set Up Apache Virtual Hosts on CentOS 7” tutorial for Vhosting with an SSL

Oh I think I figured it out. The second URL doesn’t have an https. Ill report back if it works.

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.

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.