404 routing issue - AWS Linux / Wordpress site - linux

We are running a WordPress site on a AWS EC2 Linux instance and have created custom 403 and 404 error files (both html files). The files are located in the root of the website.
The root .htaccess file has been amended to include the following section:
# Custom Documents
ErrorDocument 403 /403.html
ErrorDocument 404 /404.html
The site uses a theme and within this there is a 404.php file. The problem is that any 404 requests are being routed to this file instead of the 404.html file.
The httpd.conf file has all access set for the root folder var/www/html so there’s nothing in it that I can see that would be blocking the .htaccess file. As a double check I added some 301 redirects into the .htaccess file just to check if they were getting hit and they were working.
Also checked if there were any other .htaccess files which might be overriding the one in , there were some in a plugin but from what I can see they shouldn’t have any impact.
Is there anything else which could be overriding the .htaccess file?

ErrorDocument 404 doesn't work in Wordpress like regular pages. Wordpress by default shows 404.php content from inside your selected theme. You can put your custom code in that 404.php file.
Here is official Wordpress guide for Creating an Error 404 Page

Related

How to make 404 error on homepage?

I have an empty domain, and would like to return a 404 error when people go to the homepage, or any other URL. Currently, when I go to the domain, I'm being displayed the empty directory.
How can I achieve that?
I'm being displayed the empty directory.
I assume you mean you are being shown an (Apache generated) index of that directory (which would appear to be empty). This is a "feature" of Apache. To disable directory indexes then include the following at the top of the .htaccess file in your document root:
Options -Indexes
However, this will result in a 403 Forbidden being served when accessing a directory that does not contain a DirectoryIndex document (because server-generated directory indexes are "forbidden"). To explicitly return a 404 for all requests, include the following below the above in the .htaccess file:
RewriteEngine On
RewriteRule ^ - [R=404]
This uses mod_rewrite to instruct Apache to return a 404 status (default error page) for all requests.
However, this does assume that you have not defined a custom 404 ErrorDocument (otherwise you'll get a 500 error resulting from a rewrite loop).

a custom 404 page for all my subdomains.

I want all my subdomains to have the same custom 404 error page. In the htaccess file of the htdocs folder I have tried the following solutions:
ErrorDocument 404 /www/errordocument/404.php
ErrorDocument 404 www/errordocument/404.php
ErrorDocument 404 http://www.mydomain.com/errordocument/404.php
None of them seems to work except for the last one. Only this one gives a 302 redirect and changes the url in the address bar. Which I don't want. I can make a 404 file + htaccess file in all the subdomains only than if someone goes to a non existing subdomain they dont get the custom 404.
how can this be done?
note: server has Apache 2.0 handler
You can keep /errordocument/ folder directly under DocumentRoot of parent domain and then have this directive in the .htaccess of parent domain:
ErrorDocument 404 /errordocument/404.php
After this under all the sub domain's DocumentRoot folder (which will be a sub directory under parent's DocumentRoot) create symbolic links like this:
cd sub1; ln -s ../errordocument; cd -
cd sub2; ln -s ../errordocument; cd -

Use 404 ErrorDocument that is in a sub directory

I have a htaccess file in a sub folder like so:
/subfolder/.htaccess
In the htaccess file the following code to set a 404 ErrorDocument
ErrorDocument 404 /404.php
The issue is the 404 page in the root directory is loaded rather than the one in the /subfolder/ folder. I can't simply change the htaccess file to use /subfolder/404.php because the system is going to be installed in lots of different places.
Is there a way to reference the files in the directory that the htaccess is in?
I just had the same problem and found you page...If you have domain/folder and want to redirect anything from the folder back to the domain's index, use ErrorDocument 404 /
...I'm not sure if this goes to the root folder or one folder up, but in your case (and mine) they are one in the same.
Hope this helps!

404 custom error template .htaccess

Ok, I have a problem, my .htaccess code is:
RewriteEngine on
ErrorDocument 404 /notfound.html
and I have not found.html near .htaccess.
Why is it not working? I don't believe what I'm seeing. I took the tutorial from here
http://www.htaccessbasics.com/404-custom-error-page/
I just want that every time I access www.site.com/ajfasoijfiajsfijaofij to show what is it in notfound.html.
when using ErrorDocument , the file you're specifying is relative to DOCUMENT ROOT not .htaccess location!
Edit:
assume you have notfound.html and you want to use it for folder dir01, you create an htaccess file inside this folder and add this:
ErrorDocument 404 /dir01/notfound.html
If you want to access your file(notfound.html) in browser it would be http://www.example.com/dir01/notfound.html. that's all I could explain!
You can use in your .htaccess file the following code:
RewriteBase /

404 errors being redirected to the homepage instead of custom 404 page

I have a site hosted on an Apache server. I have created some custom error pages and the following text at the top of my .htaccess file:
ErrorDocument 404 404.html
ErrorDocument 500 500.html
ErrorDocument 401 401.html
I have also tried,
ErrorDocument 404 /404.html
ErrorDocument 500 /500.html
ErrorDocument 401 /401.html
Both the htaccess file and the custom pages are in the root directory of the server.
The problem is that when I enter a garbage url (where I would expect to see my custom 404 page) I'm simply being redirected to my index page.
Try if your server is properly set up to parse and process .htaccess files in the first place (i.e., check if AllowOverride + AccessFileName directives are correct). For example, write some stuff in that you know will work and look if it actually gets executed (like a ridiculous rewrite rule). Also, look up your httpd log files for errors.
If it does get executed properly, the problem might be that your server is setup not to allow all kinds of overrides with .htaccess files. Your syntax however, is basically correct.

Resources