404 Redirect or better option - .htaccess

I have the following code in my htaccess file to handle 404 codes:
ErrorDocument 404 https://www.mywebsite.co.uk/lost.php
RewriteEngine On
And of course a custom 404 page is returned (works fine).
I did a nibbler check and it said the following:
It is a common mistake to setup missing page handling by using a
redirect. The missing page should directly return a 404 error and not
redirect to another page.
This website does not return a 404 error HTTP status code for missing
pages. This is bad because search engines like Google might mistake
this for a real page of content.
What is the best practice to handle 404 error codes if the above is no good? I am just wanting something generic that is a catch all. No need to use a 301 because the point of my 404 is just to let visitors know that what they are looking for is not there or they have made a typo.
If it is a "common mistake" to to use a redirect how should it be done?
Thank you for any help.

You need to remove https:// or http:// from 404 handler otherwise it forces Apache to do a full redirect instead of internal rewrite.
So you can use:
ErrorDocument 404 /lost.php

It depends. If you have an application running on the webserver that takes in every URL coming in and has some logic implemented, that by itself has to figure out if the URL exists, then the application should directly return a 404 error page under the given URL and not redirect to another page like 404.html or so. But if your webserver handles the url requests with the .htaccess file then it is OK to setup a document that is returned in case of an invalid URL.

Related

.htaccess, displaying 404 page without changing the user-entered url (no redirection)

So, my problem goes like this:
I created a custom 404 error page (error.html) and I want it to be displayed without actual redirection to the page, thus, keeping the user-entered URL. Examples:
User puts "http ://localhost/whatever.the.url/would/be" in their address bar.
error.html displays, but the URL in the address bar remains as entered by user: "http
://localhost/whatever.the.url/would/be"
I tried other people's guides and answers but they didn't work for me, perhaps because I'm really amateur and I have no idea how .htaccess actually works, sorry.
In addition to that, my .htaccess currently looks like this:
ErrorDocument 404 http://localhost/www.minimalistik.de/fejle/404/fejl.html/
Yeah it isn't much. Thanks to everyone who'd help me with this :)
To serve the error document via an internal subrequest (ie. the URL in the browser's address bar does not change) then simply specify a root-relative URL (starting with a slash) to your error document in the ErrorDocument directive (this is generally considered "normal" behaviour).
For example, if your error.html file is in the /error-documents subdirectory off the document root then you would use the following:
ErrorDocument 404 /error-documents/error.html
The user is not externally redirected to the error document.
ErrorDocument 404 http://localhost/www.minimalistik.de/fejle/404/fejl.html/
However, when you specify an absolute URL (ie. with scheme + hostname) as in this example then Apache triggers a 302 (temporary) redirect to the error document and the user is externally redirected. No "404 Not Found" response is returned to the client, unless the error document itself is returning a 404 status. And if it's returning a 200 OK (the default) then the error document itself could potentially be indexed by search engines! Needless to say, this is not recommended and is an error in most scenarios.
Reference:
https://httpd.apache.org/docs/2.4/mod/core.html#errordocument

403 redirects doesn't work but 404 does. What's wrong, what can I do?

First post around here !
I'm currently working on a website with Wamp and I am having an issue with 403 redirects. I would like to redirect all these 403 errors to a custom page.
In my .htaccess (the file is in www/project with my index.php) I have this two lines:
ErrorDocument 404 /project/views/error.php
ErrorDocument 403 /project/views/error.php
The thing is the 404 errors are perfectly redirected to the error.php page so it seems the path to the custom page is correct but it doesn't work for the 403 errors and still displays me the Apache default error page :
Forbidden
You don't have permission to access (a URL that return 403) on this server.
Please can you help me to understand what's happening because I really dont know why it doesn't work and I've seen lot of similar questions but no good answers yet.
Thank you

Is there a way to redirect without changing url?

I want to redirect my users to
www.spredt.com/404
But still having the url look the same as they typed it in the first place.
For example is someone entered www.spredt.com/93817218 and that page
doesn't exist in my directory it will redirect them but it will change
the url to www.spredt.com/404
I want the same /93817218 to stay unchanged in the url but still redirect them to the error document 404.
Right now I'm handling redirection with the .htaccess file.
ErrorDocument 500 https://www.spredt.com/500
ErrorDocument 404 https://www.spredt.com/404
ErrorDocument 403 https://www.spredt.com/403
But this changes the url.
I presume that you are redirecting your user in the 404 endpoint.
But you can just return the 404 page template when they enter a false url, and not redirect them.
The url will stay the same and they will see your 404 page.
I can't provide more detail because I don't have your code nor the langage.
EDIT :
In PHP you can do that to redirect and stay on the same URL :
include('http://www.spredt.com/404');
exit();
I can't test it right now, but it should work.
Source : https://bytes.com/topic/php/answers/545026-redirect-without-changing-address-your-browser

Using Joomla, htaccess won't forward pages without an extension to the correct 404 page?

At LTCperformance.com, I've created a custom 404 page. If the user types in ltcperformance.com/fakepage.html, it forwards to the 404 page. But if there's no extension (ltcperformance.com/fakepage), it simply shows a default system 404 page.
I'm controlling the 404 page using htaccess:
ErrorDocument 404 http://ltcperformance.com/404.php
ErrorDocument 403 http://ltcperformance.com/404.php
ErrorDocument 500 http://ltcperformance.com/404.php
I have URL Rewriting in Joomla Administrator = on
Also, in Joomla Administrator, the Adds Suffix to URL = off
Any ideas? I've gone through every answer I can find on other posts and nothing will bring up my custom 404 page if there isn't an extension on the file.
ADDITIONAL INFO:
Any non-existent pages go to the homepage when I do these settings:
- Search Engine Friendly URLs / NO
- Use URL rewriting /Yes
- Adds Suffix to URL /No
I have someone taking a look at it on the server side, but I don't know what server issue it is - everybody online says it's a server issue but the support can't pinpoint what the actual server issue is. It's Godaddy; I did set their 404 page settings (they have a separate place to put it) to my 404 page, but that didn't work either.
Joomla .htaccess routes all requests to the index.php in order to support SEF urls.
In case it can't route a page, it will load the templates' error.php page. You can edit that to your requirements, this will be the easiest.
Should the error.php not be included in your template, copy the one in /templates/system to your template folder and customize it.

404 page returns 200 ok

If i type www.mydomain/error404.html directly in to browser bar should it return a 404 responce or 200 ok?
if i type www.mydomain.co.uk/something-that-dosent-exist.html, it returns the 404 page with the correct status as 404, however should my actual 404.html page return 200ok??
http://gsitecrawler.com/tools/Server-Status.aspx
domain http://www.novahealthy.co.uk
error doc error404.html
htaccess
AddType x-mapp-php4 .htm
#turn off the smart spelling matching feature.
CheckSpelling off
#set your real error document, which can be a php document.
ErrorDocument 404 /error404.html
host is 1and1 shared hosting
cheers for any help lee
Yes, that's correct. If you're going to any world-accessible site and your browser successfully gets that page you should have 200 OK returned.
Typically you don't keep error pages in a place that a user is able to directly access. You usually internally redirect to an error page on the server.
I would expect it to return a 200. Your page (even though it's named 404, and is being used as your redirect 404 page) exists, and it was found and served correctly.

Resources