smeehan
By:
smeehan

sites-available and sites-enabled?

December 13, 2016 2.7k views
Apache Ubuntu 16.04

Hi

What's the difference between sites-available and sites-enabled?

I'm having some difficulty getting either .htaccess or mod_rewrite to work. I'm not sure which bit is failing at the moment.

I been reading two guides

'How To Use the .htaccess File' tells me to open /etc/apache2/**sites-available**/default which is empty (?), according to the guide it shouldn't be.

And 'How To Set Up mod_rewrite for Apache on Ubuntu 14.04' tells me to open /etc/apache2/**sites-enabled**/000-default.conf

Am I supposed to be editing both these files?

Bit confused :)

1 comment
  • I can understand the confusion. If Apache is configured properly they are the same file (not a copy). The idea here is that all your sites are defined by files in the sites-available folder and then you can use the command a2ensite mysite (where the file is called mysite.conf) and the system creates a symlink on the filesystem in the sites-enabled folder. Apache when starting actually reads the configuration from any .conf files (or symlinks to .conf files) in the sites-enabled folder.

    To get .htaccess files to work properly you need to have an AllowOverride All set somewhere in your configuration inside a <Directory> block that points to the location where you want this to take effect.

    As @JasonLabs mentioned it will look somewhat like this:

    <Directory /var/www/html/mysite>
            AllowOverride All
    </Directory>
    
3 Answers

I had a rough time with this earlier, and I can't find the source of this information right now. I will keep looking, but...

To enable .htaccess to your web directory on Ubuntu 16.04, the file you need to edit is located at:

/etc/apache2/apache2.conf

Towards the middle/lower end of the file you will see:

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

If you have AllowOverride None, change it to AllowOverride All

This turns on .htaccess for your web directory.

From here you should be able to follow the instructions for Enabling mod_rewrite from here: https://www.digitalocean.com/community/tutorials/how-to-set-up-mod_rewrite-for-apache-on-ubuntu-14-04

Which should be as follows:

sudo a2enmod rewrite
sudo systemctl restart apache2

Mine was already enabled I believe.

In this tutorial, we will activate and learn how to manage URL rewrites using Apache2's `mod_rewrite` module. This tool allows us to rewrite URLs in a cleaner fashion, translating human-readable paths into code-friendly query strings. This guide is split into two halves: the first sets up a sample web application and the second explains commonly-used rewrite rules.

Thanks for the replies!

This is the contents of my /etc/apache2/apache2.conf

I can't see the lines @JasonLabs mentions?

Should I just add them?

# This is the main Apache server configuration file.  It contains the
# configuration directives that give the server its instructions.
# See http://httpd.apache.org/docs/2.4/ for detailed information about
# the directives and /usr/share/doc/apache2/README.Debian about Debian specific
# hints.
#
#
# Summary of how the Apache 2 configuration works in Debian:
# The Apache 2 web server configuration in Debian is quite different to
# upstream's suggested way to configure the web server. This is because Debian's
# default Apache2 installation attempts to make adding and removing modules,
# virtual hosts, and extra configuration directives as flexible as possible, in
# order to make automating the changes and administering the server as easy as
# possible.

# It is split into several files forming the configuration hierarchy outlined
# below, all located in the /etc/apache2/ directory:
#
#       /etc/apache2/
#       |-- apache2.conf
#       |       `--  ports.conf
#       |-- mods-enabled
#       |       |-- *.load
#       |       `-- *.conf
#       |-- conf-enabled
#       |       `-- *.conf
#       `-- sites-enabled
#               `-- *.conf
#
#
# * apache2.conf is the main configuration file (this file). It puts the pieces
#   together by including all remaining configuration files when starting up the
#   web server.
#
# * ports.conf is always included from the main configuration file. It is
#   supposed to determine listening ports for incoming connections which can be
#   customized anytime.
#
# * Configuration files in the mods-enabled/, conf-enabled/ and sites-enabled/
#   directories contain particular configuration snippets which manage modules,
#   global configuration fragments, or virtual host configurations,
#   respectively.
#
#   They are activated by symlinking available configuration files from their
#   respective *-available/ counterparts. These should be managed by using our
#   helpers a2enmod/a2dismod, a2ensite/a2dissite and a2enconf/a2disconf. See
#   their respective man pages for detailed information.
#
# * The binary is called apache2. Due to the use of environment variables, in
#   the default configuration, apache2 needs to be started/stopped with
#   /etc/init.d/apache2 or apache2ctl. Calling /usr/bin/apache2 directly will not
#   work with the default configuration.


# Global configuration
#

#
# ServerRoot: The top of the directory tree under which the server's
# configuration, error, and log files are kept.
#
# NOTE!  If you intend to place this on an NFS (or otherwise network)

Ok, I'm not 100% sure how it works, but I've been able to get Step 4 — Setting Up Files from the guide working.

I'll have to come back to this when I setup another Droplet, so I know exactly what I'm doing :)

This test rewrite works

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^about-test$ about-test.html [NC]
</IfModule>

If I visit example.com/about-test it shows the content of about-test.html and the url doesn't have the .html extension.

So at least I know mod_rewrite and my .htaccess file is working...

That said, a set of rewrite rules I use on other hosting setups (that work fine) aren't working...

In this tutorial, we will activate and learn how to manage URL rewrites using Apache2's `mod_rewrite` module. This tool allows us to rewrite URLs in a cleaner fashion, translating human-readable paths into code-friendly query strings. This guide is split into two halves: the first sets up a sample web application and the second explains commonly-used rewrite rules.
Have another answer? Share your knowledge.