.htaccess ErrorDocument 404 not showing up

Apache.HtaccessAmazon Ec2Amazon Web-ServicesApache Config

Apache Problem Overview


I have a server from AWS EC2 service running on Linux ubuntu and I have installed apache, php, and mysql.

I have added a .htaccess file in my document root /var/www/html.

I entered this code in it: ErrorDocument 404 /var/www/html/404.php and it is still not showing up.

I kept entered this command multiple times: sudo service httpd restart to restart the server but no changes displayed...

How can I fix this... Did I do something wrong?

Apache Solutions


Solution 1 - Apache

First, note that restarting httpd is not necessary for .htaccess files. .htaccess files are specifically for people who don't have root - ie, don't have access to the httpd server config file, and can't restart the server. As you're able to restart the server, you don't need .htaccess files and can use the main server config directly.

Secondly, if .htaccess files are being ignored, you need to check to see that AllowOverride is set correctly. See http://httpd.apache.org/docs/2.4/mod/core.html#allowoverride for details. You need to also ensure that it is set in the correct scope - ie, in the right block in your configuration. Be sure you're NOT editing the one in the block, for example.

Third, if you want to ensure that a .htaccess file is in fact being read, put garbage in it. An invalid line, such as "INVALID LINE HERE", in your .htaccess file, will result in a 500 Server Error when you point your browser at the directory containing that file. If it doesn't, then you don't have AllowOverride configured correctly.

Solution 2 - Apache

  1. Enable Apache mod_rewrite module

    a2enmod rewrite

  2. add the following code to /etc/apache2/sites-available/default

    AllowOverride All

  3. Restart apache

    /etc/init.d/apache2 restart

Solution 3 - Apache

If you have tried all of the above, which are all valid and good answers, and your htaccess file is not working or being read change the directive in the apache2.conf file. Under Ubuntu the path is /etc/apache2/apache2.conf

Change the <Directory> directive pointing to your public web pages, where the htaccess file resides. Change from AllowOverride None to AllowOverride All

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

I had the same problem and found the answer and explanation on the Ubuntu Ask! forum https://askubuntu.com/questions/421233/enabling-htaccess-file-to-rewrite-path-not-working

Solution 4 - Apache

For Ubuntu,
First, run this command :-

sudo a2enmod rewrite

Then, edit the file /etc/apache2/sites-available/000-default.conf using nano or vim using this command :-

sudo nano /etc/apache2/sites-available/000-default.conf

Then in the 000-default.conf file, add this after the line DocumentRoot /var/www/html. If your root html directory is something other, then write that :-

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

After doing everything, restart apache using the command sudo service apache2 restart

Solution 5 - Apache

Most probably, AllowOverride is set to None. in Directory section of apache2.conf located in /etc/apache2 folder

Try setting it to AllowOverride All

Solution 6 - Apache

Just follow 3 steps

  1. Enable mode_rewrite using following command

    sudo a2enmod rewrite

Password will be asked. So enter your password

  1. Update your 000-default.conf or default.conf file located at /etc/apache2/sites-available/ directory. you can not edit it directly. so use following command to open
sudo gedit /etc/apache2/sites-available/000-default.conf

Or sudo gedit /etc/apache2/sites-available/default.conf

you will get

> DocumentRoot /var/www/html

OR

> DocumentRoot /var/www

line. Add following code after it.

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

Make user the directory tag path is same as shown in your file.

>

  1. Restart your apache server using following command

    sudo service apache2 restart

Solution 7 - Apache

In my experience, /var/www/ directory directive prevents subfolder virtualhost directives. So if you had tried all suggestions and still not working and you are using virtualhosts try this ;

1 - Be sure that you have AllowOverride All directive in /etc/apache2/sites-available/example.com.conf

2 - Check /var/www/ Directory directives in /etc/apache2/apache2.conf (possibly at line 164), which looks like ;

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

If there is an AllowOverride None directive change it to AllowOverride All or just escape line

Solution 8 - Apache

By default, Apache prohibits using an .htaccess file to apply rewrite rules, so

Step 1 — Enabling mod_rewrite (if not Enabled) First, we need to activate mod_rewrite. It's available but not enabled with a clean Apache 2 installation.

$ sudo a2enmod rewrite

This will activate the module or alert you that the module is already enabled. To put these changes into effect, restart Apache.

$ sudo systemctl restart apache2

mod_rewrite is now fully enabled. In the next step we will set up an .htaccess file that we'll use to define rewrite rules for redirects.

Step 2 — Setting Up .htaccess Open the default Apache configuration file using nano or your favorite text editor.

$ sudo nano /etc/apache2/sites-available/000-default.conf

Inside that file, you will find a block starting on the first line. Inside of that block, add the following new block so your configuration file looks like the following. Make sure that all blocks are properly indented.

/etc/apache2/sites-available/000-default.conf

<VirtualHost *:80>
    <Directory /var/www/html>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>

    . . . 
</VirtualHost>

Save and close the file. To put these changes into effect, restart Apache.

$ sudo systemctl restart apache2

Done. Your .htacess should work. This link may actually help somebody https://www.digitalocean.com/community/tutorials/how-to-rewrite-urls-with-mod_rewrite-for-apache-on-ubuntu-16-04

Solution 9 - Apache

Go to /etc/apache2/apache2.conf

You have to edit that file (you should have root permission). Change directory text as bellow: >
> Options Indexes FollowSymLinks
> AllowOverride All
> Require all granted
>

Now you have to restart apache.

service apache2 restart

Solution 10 - Apache

I cleared this use. By using this site click Here , follow the steps, the same steps follows upto the ubuntu version 18.04

Solution 11 - Apache

In WampServer Open WampServer Tray icon ----> Apache ---> Apache Modules --->rewrite_module

Solution 12 - Apache

use RewriteBase /{your folder}/ on your .htaccess

Solution 13 - Apache

For completeness, if "AllowOverride All" doesn't fix your problem, you could debug this problem using:

  1. Run apachectl -S and see if you have more than one namevhost. It might be that httpd is looking for .htaccess of another DocumentRoot.

  2. Use strace -f apachectl -X and look for where it's loading (or not loading) .htaccess from.

Solution 14 - Apache

i have a lot of sites on my virtual machine, and i solved it only by changing config of the site in which i needed .htaccess

what i did:

  1. sudo a2enmod rewrite

next i changed only config for particular site, not for every site "example.com"

  1. sudo nano /etc/apache2/sites-enable/example.com.conf

inside of it i added

<Directory /var/www/example.com>
    AllowOverride All
</Directory>

3) service apache2 restart

so it only applies for 1 site, because when i tried to apply changes to entire server it crashed, don't know why, but this solved my problem

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShalin ShahView Question on Stackoverflow
Solution 1 - ApacheRich BowenView Answer on Stackoverflow
Solution 2 - ApacheSijo Thomas MaprayilView Answer on Stackoverflow
Solution 3 - Apacheuser1946891View Answer on Stackoverflow
Solution 4 - ApacheDeval KhandelwalView Answer on Stackoverflow
Solution 5 - ApacheKawaiKxView Answer on Stackoverflow
Solution 6 - ApacheRameshwer DhanoraView Answer on Stackoverflow
Solution 7 - ApacheErdinç ÇorbacıView Answer on Stackoverflow
Solution 8 - ApacheAbayomi IsraelView Answer on Stackoverflow
Solution 9 - ApacheShamal SabahView Answer on Stackoverflow
Solution 10 - ApacheRaghul SKView Answer on Stackoverflow
Solution 11 - ApacheBruceView Answer on Stackoverflow
Solution 12 - Apachemiftah al rasyidView Answer on Stackoverflow
Solution 13 - ApachegilmView Answer on Stackoverflow
Solution 14 - ApacheoruchkinView Answer on Stackoverflow