ChatGPT解决这个技术问题 Extra ChatGPT

.htaccess ErrorDocument 404 not showing up

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?

Are you sure you want to map the 400 Bad Request error to /var/www/html/404.php?

R
Rich Bowen

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.


+1 for putting garbage in the .htaccess file as a diagnostic tool.
Garbage in programming is more useful than it sounds.
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 works
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#allowoverride
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.
D
Danny Sofftie

Enable Apache mod_rewrite module a2enmod rewrite add the following code to /etc/apache2/sites-available/default AllowOverride All Restart apache /etc/init.d/apache2 restart


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/
Thanks, in my case the rewrite was not enable by default in apache2
i already follow this steps but it is not working in apache 2.4 what i am doing wrong =[
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!
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?
C
Community

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


Or, it can be added on the VHost
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).
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.
D
Deval Khandelwal

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


K
KawaiKx

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

Try setting it to AllowOverride All


s
shafiq.rst

Just follow 3 steps

Enable mode_rewrite using following command sudo a2enmod rewrite

Password will be asked. So enter your password

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.

Restart your apache server using following command sudo service apache2 restart


Thanks. Greetings from Brazil.
E
Erdinç Çorbacı

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


A
Abayomi Israel

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


The restart command that worked for me is $ sudo httpd -k restart (Red Hat 4.4.7-23)
S
Shamal Sabah

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


R
Raghul SK

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


B
Bruce

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


g
gilm

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

Run apachectl -S and see if you have more than one namevhost. It might be that httpd is looking for .htaccess of another DocumentRoot. Use strace -f apachectl -X and look for where it's loading (or not loading) .htaccess from.


o
oruchkin

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:

sudo a2enmod rewrite

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

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

inside of it i added

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

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


关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now