61

In almost every solution of "How to activate htaccess", they say that the /etc/apache2/sites-available/default file needs to be edited. But there isn't such file in Apache 2.4.7

I read somewhere that the new default file is 000-default.conf. So I edited that one and tried to add the line:

AllowOverride All

But Apache2 did not restart correctly and gave an error. From an apache2 documentation, I found that AllowOverride is only allowed under the <Directory> section. Then I tried adding this:

<Directory "/var/www">
AllowOverride All
</Directory>

And this seems to work. But I am not sure if I should have put /var/www there. Is it a correct way of doing it or will my computer blow up somehow?

CluelessNoob
  • 2,383

6 Answers6

72

tl;dr

Yes it's the correct way.
But to be more precise: Yes, it's the correct way to allow .htaccess to override all directives in the /var/www directory.


As you found out, AllowOverride is allowed only under the Directory section.

Using your example:

<Directory "/var/www">
    AllowOverride All
</Directory>

This is telling apache, that all configurations can be overridden in the /var/www and all its sub-directories (recursively).


For a better example, consider you have the following configuration in your virtual host:

<Directory "/var/www">
    AllowOverride All
</Directory>

<Directory "/var/www/uploads"> AllowOverride Limit </Directory>

And the following directory structure:

var/
    www/
        .htaccess
        uploads/
            .htaccess
            a/
                .htaccess
            b/
                .htaccess
        code/
            .htaccess
            c/
                .htaccess
            d/
                .htaccess

What I did here, is create an .htaccess in every sub-directory of the /var/www directory.
It usually shouldn't be like so, but this is just for the sake of the example

Comparing the directory structure with the configuration, it means that all .htaccess files inside in the /var/www folder and its sub-directories, excluding the /var/www/uploads directory and its sub-directories, can override all kinds of directives.

But /var/www/uploads and its sub-directories can only use the .htaccess file to override the Allow, Deny and Order directives.

Note: As of apache 2.4 (Which is available by default in 13.10+) the Allow, Deny and Order directives were replaced by a single directive named Require.

Dan
  • 14,180
48

First enable rewrite using this command:

sudo a2enmod rewrite

Then restart apache2:

sudo service apache2 restart

Then go into the sites-available folder:

/etc/apache2/sites-available

Edit the file named default and change AllowOverride none to AllowOverride All. There are two lines where this change has to be made.

This will make .htaccess work in your server VPS.

This worked on an Ubuntu 12.04.5 VPS.

Eliah Kagan
  • 119,640
q8fft
  • 481
  • 4
  • 2
4

In my case, it worked like this:
I had to add the following lines:

Order allow, deny
Allow from all

So it looks like this:

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Order allow,deny
    allow from all
</Directory>
Dan
  • 14,180
3

Here a snippet how to enable htaccess in Apache 2.4 without change the default configuration:

cat <<EOF> /etc/apache2/conf-available/allow-override.conf
<Directory "/var/www">
    AllowOverride all
</Directory>
EOF

a2enconf allow-override
service apache2 reload
panticz
  • 1,936
2

A common problem i ran into in these tutorials is that there's no default.conf file in

/etc/apache2/sites-available

I was able to find the place to change it,but it was the apache2.conf file here

/etc/apache2/apache2.conf

Not really sure if this is a good idea, but it did work for me, and in the environment I'm in it's safe.

1

For those running apache 2.4 and not finding "default" look in /etc/apache2/apache2.conf and edit

Directory

AllowOverride All

Directory


Directory /var/www/>

AllowOverride All

Directory

Simon Sudler
  • 4,111
Mextro
  • 51