.htaccess ErrorDocument 404 not showing up
Asked Answered
I

13

155

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?

Impenetrable answered 30/8, 2012 at 17:33 Comment(1)
Are you sure you want to map the 400 Bad Request error to /var/www/html/404.php?Majoriemajority
W
317

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.

Whaleback answered 30/8, 2012 at 19:1 Comment(5)
+1 for putting garbage in the .htaccess file as a diagnostic tool.Catnap
Garbage in programming is more useful than it sounds.Djakarta
I putted "A STRING" in my .htaccess, so I'd receive a error, so I putted AllowOverride All in all apache2.conf in my Debian and it worksVerse
Note that AllowOverride only works in <Directory> directives and will be ignored if placed inside a <Location ...> section, see: httpd.apache.org/docs/2.4/mod/core.html#allowoverrideRetrogressive
Also note that the <Directory> directive expects an absolute path instead of relative. In my case, <Directory /> failed while <Directory /var/www/html/subdir> worked.Pudendum
D
133
  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

Dilley answered 16/10, 2013 at 6:27 Comment(8)
note that in Apache 2.4 the configuration files under /etc/apache2/sites-available must end in .conf now. And I had some mixup on my server with a default.conf in /etc/apache2/sites-available and one in /etc/apache2/Biblical
Thanks, in my case the rewrite was not enable by default in apache2Prickly
i already follow this steps but it is not working in apache 2.4 what i am doing wrong =[Coparcener
Looks like require ip overrides require authentication, this behavior are unexpected (allow from certain ip in apache 2.2 dont do this) and in my opinion can be very dangerous, because the ip exception are anulating the authentication, in apache 2.2 they are completely different things!Coparcener
I omitted the first step but did change AllowOverride to All in my apache.conf (Debian 8, /etc/apache2/apache.conf), restarted and the files are not accessible, good. But the error is not like permission denied but 500 Internal Server Error, why is that? Can I fix it?Pyle
above restart command didn't worked for me, I used $ sudo httpd -k restart (in Red Hat 4.4.7-23)Treen
@LucianoAndressMartini I had the same issue with apache 2.4 on ubuntu 18, I couldn't start apache2, I got Action 'start' failed., the log at tail /var/log/apache2/error.log says Function not implemented: AH00023: Couldn't create the rewrite-map mutex, then I fixed that by uncomment Mutex file:${APACHE_LOCK_DIR} default at /etc/apache2/apache2.conf config file. (this issue doesn't appear with debian)Griefstricken
Thanks, in my case the rewrite was not enable by default in apache2Sharper
P
54

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

Psoriasis answered 18/9, 2015 at 15:30 Comment(3)
Or, it can be added on the VHostImprescriptible
Actually, only AllowOverride FileInfo is required in 3rd line for .htaccess file to be read and used. All also enables additional configuration overrides, such as .httpasswd (which can be enabled individually by using Authconfig instead of All).Salutatory
Adding AllowOverride All solved one problem for me, but introduced another - now my server isn't parseing the PHP but instead printing it in cleartext.Nicole
S
24

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

Sundew answered 20/4, 2016 at 11:15 Comment(0)
C
6

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

Try setting it to AllowOverride All

Catania answered 3/10, 2016 at 5:57 Comment(0)
M
6

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

Myriapod answered 2/7, 2017 at 7:34 Comment(1)
Thanks. Greetings from Brazil.Gateshead
C
6

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

Cultus answered 5/3, 2018 at 13:27 Comment(0)
H
5

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

Hominoid answered 4/8, 2018 at 19:29 Comment(1)
The restart command that worked for me is $ sudo httpd -k restart (Red Hat 4.4.7-23)Treen
S
2

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

Simpson answered 16/7, 2019 at 6:30 Comment(0)
M
2

Go to /etc/apache2/apache2.conf

You have to edit that file (you should have root permission). Change directory text as bellow:

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

Now you have to restart apache.

service apache2 restart

Mischief answered 10/10, 2020 at 21:38 Comment(0)
S
1

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

Solidstate answered 27/5, 2015 at 11:14 Comment(0)
F
1

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.

Faradism answered 12/1, 2020 at 13:13 Comment(0)
P
0

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>
  1. 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

Papacy answered 4/3, 2022 at 12:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.