How to enable use of .htaccess in Apache on Ubuntu?

124,584

Solution 1

First Step

Open file as

sudo vim /etc/apache2/apache2.conf

Second Step

remove comment sign (#) if you find it before this line ( line number 187 approx.)

AccessFileName .htaccess

Third Step

Then find the line where there is

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

replace "None" with "All"

AllowOverride All

And voila... .htaccess works!!

Solution 2

Activate ModRewrite:

sudo a2enmod rewrite
sudo /etc/init.d/apache2 restart

Solution 3

In my case the changing AllowOverride None to AllowOverride All in /etc/apache2/sites-enabled/000-default.conf helped.

All other .conf files have already had AllowOverride All.

Solution 4

  1. In /etc/apache2/apache2.conf remove comment sign (#) from AccessFileName .htaccess
  2. The name of your Apache configuration file is not standard. You can rename it to default.conf in Windows and rename it to default in Linux. In Linux based OS run this code in terminal:

    sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/default
    
Share:
124,584

Related videos on Youtube

David ZIP
Author by

David ZIP

Updated on September 18, 2022

Comments

  • David ZIP
    David ZIP over 1 year

    I'm trying to enable use of htaccess file in Ubuntu 14.04 (Apache 2.4.7). I know that this question has many possible duplicates, but none of them helped me yet.

    .htaccess

    ErrorDocument 404 /404.html
    

    000-default.conf

    <VirtualHost *:80>
    AccessFileName .htaccess
    
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www
    
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    
    <Directory /var/www/>
      Options Indexes FollowSymLinks Includes
      AllowOverride All
      Order allow,deny
      Allow from all
    </Directory>
    </VirtualHost>
    

    What else is needed?

    • closetnoc
      closetnoc about 10 years
      The AllowOverride All directive allows all options in the .htaccess file. The 000-default.conf assumes that there is only one site on your server. Is the .htaccess file not working for you? Are there more than one site hosted on this server?
    • David ZIP
      David ZIP about 10 years
      There are many folders in /var/www but no virtual hosts are created. /etc/apache2/sites-available/ folder contains two files, 000-default.conf and default-ssl.conf. htaccess file is located in /var/www/
    • Kevin Woblick
      Kevin Woblick about 10 years
      Maybe 'DocumentRoot /var/www/'? (trailing slash)
    • Stephen Ostermiller
      Stephen Ostermiller about 10 years
      Check to make sure /etc/apache2/sites-enabled/ contains a link to 000-default.conf. If you change the /var/www/index.html file you can see the changes when you view the server with a web browser, right?
  • Stephen Ostermiller
    Stephen Ostermiller about 10 years
    It is named with the zeros so that it comes first alphabetically. If you have a default.conf file and then create a site for anteater.com, then anteater becomes the default host because it gets loaded first. Keep the 000 to prevent such problems. The name of the conf file shouldn't have anything to do with whether or not .htaccess works.
  • David ZIP
    David ZIP about 10 years
    'AccessFileName .htaccess' is already uncommented. Above answer helped me. Thanks.
  • Stephen Ostermiller
    Stephen Ostermiller over 9 years
    The service command is now the preferred way to restart Apache: sudo service apache2 restart.
  • Tahir Yasin
    Tahir Yasin over 9 years
    changes didn't take effect until I restarted apache Command: service apache restart
  • www139
    www139 over 7 years
    I suggest, as @TahirYasin mentions, restarting apache. sudo service apache2 restart.