Using 404 error pages correctly

tiger,

TB is quite correct as that is the syntax for ErrorDocument handling by Apache.

Apache also has a little-used module which can correct a limited number of typos and capitalization errors: mod_speling (their spelling error is said to be intentional - an inside joke). IMHO, this is a pretty user-friendly module to be using.

IMHO, mod_rewrite can also be used to send the errant request to an error handling script and include the original request to help the handler redirect to either your sitemap or a similar page.

AT THE END OF YOUR mod_REWRITE code:

# Prior code should have already performed a redirection to a file (or a directory) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule .? error_handler.php?request=%{REQUEST_URI} [L]

This snippet checks whether the request is a file or a directory and, if neither, redirects to the error_handler script with the request in the $_GET array. What you do with that is a mark of your webmaster skills but could include a brief message and a timed redirection to your sitemap up to changing to request to lowercase and then parsing it for key words before making a redirection to the closest key word (else to the sitemap).

Edit: If you are concerned about a prior redirection changing {REQUEST_URI} and want to use {THE_REQUEST} instead (in the RewriteRule), feel free to change the Apache variable used in the redirection.

IMHO, be kind to your visitors but without going overboard (i.e., don’t waste too much effort “handholding”) will help visitors to your website. Use whichever technique best suits your temperament and website.

Regards,

DK

Thank you all for your help and advice. thus far it has been very useful.
Excuse my obtuseness but I an struggling with the difference between the following snippets

Redirect 301 /index.html /index.php

and

Redirect 301 index.html index.php

I have the .htaccess file in the same directory as the (old) .htm and the .php.
Does the presence of the leading / affect the operation of flow of code?

Hi Tigers,

Nothing obtuse about that as it’s a very valid question. The answer is that the first slash PROBABLY isn’t required but the second slash is required to make it an (internal) absolute redirection. That’s simply the required syntax for the Redirect (mod_alias) directive.

Regards,

DK

Still have some puzzlement concerned how the 301 redirect works or perhaps how I think it works.
I have assumed that the code fragment below

 redirect 301 stitcheries.htm http://www.petalsandpatches.com/stitcheries.php

would redirect stitcheries.htm to stitcheries.php.
It seems however that it actually sends the user to my custom 404 error page, which is good in a way as it means that users stay on the site. When they select the given home button it takes them to index.php.

Is that the way the 301 works viz. to an error page rather than straight to the newer page?

Unless of course I have still missing something or done something incorrect?

I believe the correct form is with a capital:

Redirect 301 /stitcheries.htm http://www.petalsandpatches.com/stitcheries.php

Linux/Unix are case sensitive, so I’m guessing redirect 301 will not work. When functioning correctly, it will redirect the old page to the new, not produce a 404 error.

I would always include the leading / in the file path. (It indicates the root directory.) @dklynn - who knows vastly more about this stuff than I do - thinks it’s optional, and if you can get it working without, then fine. But if it still doesn’t work once you’ve capitalised Redirect, that would be my next suggestion to try.

Tigers,

You appear to be jumping around between mod_rewrite and mod_alias for your redirection. Only one is required and mod_alias will always take precedence (because it is part of the Apache core) over mod_rewrite.

Thanks, TB, for the vote of confidence … despite my error with the leading slash for the URL-path!

From https://httpd.apache.org/docs/2.4/mod/mod_alias.html#redirect:

Redirect Directive

Description: Sends an external redirect asking the client to fetch a different URL
Syntax: Redirect [status] URL-path URL
Context: server config, virtual host, directory, .htaccess
Override: FileInfo
Status: Base
Module: mod_alias
The Redirect directive maps an old URL into a new one by asking the client to refetch the resource at the new location.

The old URL-path is a case-sensitive (%-decoded) path beginning with a slash {my bad!}. A relative path is not allowed.

The new URL may be either an absolute URL beginning with a scheme and hostname, or a URL-path beginning with a slash. In this latter case the scheme and hostname of the current server will be added.

Then any request beginning with URL-Path will return a redirect request to the client at the location of the target URL

However, I believe that Apache is kind enough to convert capitalization errors so they don’t effect the directive, in other words, “Redirect” will be seen when “redirect” is requested. The same, however, is NOT true for the case-sensitive URL-path or the case-sensitive URL!

If you will show your entire .htaccess code, I’m sure we can spot the conflict which is causing the redirection to your 404 script.

Regards,

DK

Here is the .htaccess as requested

# Added redirections from .htm to .php pages
Redirect 301 stitcheries.htm http://www.petalsandpatches.com/stitcheries.php
Redirect 301 pincushions.htm http://www.petalsandpatches.com/pincushions.php
Redirect 301 ordering.htm http://www.petalsandpatches.com/ordering.php
Redirect 301 quilts.htm http://www.petalsandpatches.com/quilts.php
Redirect 301 applique.htm http://www.petalsandpatches.com/applique.htm
Redirect 301 chalkboards.htm http://www.petalsandpatches.com/chalkboards.php
Redirect 301 accessories.htm http://www.petalsandpatches.com/accessories.php
Redirect 301 seasidemoments.htm http://www.petalsandpatches.com/seasidemoments.php
Redirect 301 links.htm http://www.petalsandpatches.com/links.php
Redirect 301 bags.htm http://www.petalsandpatches.com/bags.php
Redirect 301 bears.htm http://www.petalsandpatches.com/bears.php
Redirect 301 stockisys.htm http://www.petalsandpatches.com/stockists.php
Redirect 301 clothdolls.htm http://www.petalsandpatches.com/clothdolls.php
Redirect 301 index.htm http://www.petalsandpatches.com/index.php
Redirect 301 blockofthemonth http://www.petalsandpatches.com/blockofthemonth.php
Redirect 301 onfreedomswings.htm http://www.petalsandpatches.com/onfreedomswings.php
Redirect 301 naturesgifts.htm http://www.petalsandpatches.com/naturesgifts.php
Redirect 301 orderform.htm http://www.petalsandpatches.com/orderform.pdf
Redirect 301 bears_viewcart.htm http://www.petalsandpatches.com/bears.php

Well, I think we’ve now established that you do, indeed, need the leading / on the old filepath, so I would start by changing that.

Kudos to TB for the leading / AND for pointing out that Tigers’ code needs this in every Redirect line.

HOWEVER, I rely on mod_rewrite so much that I would prefer to use my code in post # 13. That said, though, mod_alias is much quicker (because it doesn’t need the regex engine) so I recommend:

  1. Correct the current code (add leading /'s to the filenames and delete all the “http://www.petalsandpatches.com” from the redirections) then
  2. Run a timing test using only the long list of Redirect statements
  3. Then save that .htaccess file offline.
  4. Create a new .htaccess file with just the mod_rewrite code from post #13
  5. Run another timing test.
  6. Compare the two timing test results and use ONLY the code which provides the fastest time.

I suspect that even this lengthy list of Redirect statements will be faster (after being corrected with the leading /'s and eliminating the superfluous “http://www.petalsandpatches.com” from the redirections) but making a test will provide proof either way.

Regards,

DK

I have altered my .htaccess file as was suggested viz, removed "http://www.petalsandpatches.com"and have the leading /.
The results were not what I expected.


The code I loaded was

# Added Error Pages
ErrorDocument 403 /error-pages/403error.htm
# ErrorDocument 404 http://www.petalsandpatches.com/error-pages/404error.php
ErrorDocument 404 /error-pages/404error.php

AddHandler server-parsed .html
AddHandler server-parsed .htm

# Added redirections from .htm to .php pages
Redirect 301 /stitcheries.htm /stitcheries.php
Redirect 301 /pincushions.htm /pincushions.php
Redirect 301 /ordering.htm /ordering.php
Redirect 301 /quilts.htm /quilts.php
Redirect 301 /applique.htm /applique.htm
Redirect 301 /chalkboards.htm /chalkboards.php
Redirect 301 /accessories.htm /accessories.php
Redirect 301 /seasidemoments.htm /seasidemoments.php
Redirect 301 /links.htm /links.php
Redirect 301 /bags.htm /bags.php
Redirect 301 /bears.htm /bears.php
Redirect 301 /stockists.htm /stockists.php
Redirect 301 /clothdolls.htm /clothdolls.php
Redirect 301 /index.htm /index.php
Redirect 301 /blockofthemonth /blockofthemonth.php
Redirect 301 /onfreedomswings./onfreedomswings.php
Redirect 301 /naturesgifts.htm /naturesgifts.php
Redirect 301 /orderform.htm /orderform.pdf
Redirect 301 /bears_viewcart.htm /bears.php

Could someone point out the blunder I have committed? I don’t know whether the emboldened 1st ErrorDocument is important. It is plain text in my code.

tigers,

That all looks fine EXCEPT the image does not display and blockofthemonth is missing (?) its .htm file extension - your ErrorDocument statements are correct (assuming you have the correct path {from your DocumentRoot} and filenames)… The only thing I can think of (without seeing your test URI) is that your host must NOT allow using your .htaccess file (difficult to imagine but I remember seeing something about GoDaddy (?) years ago not enabling .htaccess files from clients.

If that isn’t working (and your URI is EXACTLY one of the listed .htm files, then give the mod_rewrite a try (post #13 if I remember correctly).

Regards,

DK

Is that error document really .htm when the rest of the site is .php?

Both of those lines are missing extensions, and no idea what that second line running together might do.

Verify that the 403 should be .htm and not .php for us.

Fix/check those things, test a url that is exactly one of those (or several of those) listed pages - including the “orderform.htm” or “index.htm” or whichever - and then we can go from there.

2 Likes

jj,

Good eyes!

If there is no space between the . and the /, then there is a syntax error which should disable the entire website (I suspect). Typos? If not, then that is the problem.

As I said, though, I tend to rely on mod_rewrite and would use that to avoid all the superfluous typing (and typos).

Regards,

DK

Et al
I have made the changes all have suggest viz. fixed 403error.htm plus the missing extension the 2 pages.

Still didn’t work then found a space needed in

Redirect 301 /onfreedomswings.htm/onfreedomswings.php

Now it is ok. If I put a .htm it redirects immediately to .php without the intermediate 404 page.
An unknown page gives the 404 error page as required.

I was rather chuffed until a kind person pointed out that my contactus page failed the test. :frowning:

I have fixed that so at present all is well. I have some friends doing some testing for me.
I will let you know the test results.

1 Like

Tigers,

jeffreylees pointed out the TWO missing spaces two posts above (#25) and I echoed his finding in the post immediately above (#26).

Edit: Missing .htm and missing space in JL’s post.

Regards,

DK

If it is any help, this is the line I use in htaccess to redirect all html pages on the site to php:-

RedirectMatch 301 (.*)\.html$ http://www.example.com$1.php
1 Like

Sam,

That, too, would work.

However, RedirectMatch would require the regex engine (as would mod_rewrite) and, for Tigers short list (19 redirections), it’s probably faster to use Redirect at he has done. I do not know the time it takes to start the regex engine but suspect it may be in the realm of 20 - 100 Redirect statements but I’ll leave it to someone else to perform the time comparison exercise.

That said, I would rely on mod_rewrite as I ALWAYS have it running (for other reasons) and know how well mod_rewrite handles simple redirections like this.

Note: It was discovered that Tigers’ problem was with the syntax of just a few of his Redirect statements (they were difficult to see - kudos to TechnoBear and jeffreylees)

Regards,

DK

Thank you all very much for your help. My last look at stats showed that 404 errors went from > 100 to <10. Those people have found my wife’s current site and she is very happy.
The suggestions were most helpful. I aim to replace with Joomla and probably host the site myself and the mod_rewrite suggestions will come in most handy.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.