Link to home
Start Free TrialLog in
Avatar of bleggee
bleggeeFlag for United States of America

asked on

Best way to handle web page Redirects and web page "Missing 404" error capture (Apache)

Hi all -  I am wondering what the best way is to:

#1 Send someone to a certain error-handling web page within a site instead of generating a 404 error in the event of a URL not found.  Something like a "missing" spec or a custom error page?

#2 Send someone to http://example.com/hello.php if they left off the extension and just typed http://example.com/hello

- Brian
ASKER CERTIFIED SOLUTION
Avatar of Insoftservice inso
Insoftservice inso
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bleggee

ASKER

Thx for the info. It's core PHP.  I presume that /pages/error/404.php is just a page within the site that I use for the Error Page when a 404 occurs?
Also, how do I send someone to http://example.com/hello.php if they left off the extension and just typed http://example.com/hello
(and not generate an error)?
If using Apache you can use one of two options:

Using mod_alias:
RedirectMatch 301 ^/hello/ http://example.com/hello.php

Open in new window

Using mod_rewrite:
RewriteCond %{REQUEST_URI} !^/hello\.php$ [NC]
RewriteRule ^hello.* http://example.com/hello.php [R=301,NC,L]

Open in new window

mod_alias uses Redirect and RedirectMatch directives whereas mod_rewrite uses Rewrite directives. Both options will do the 301 permanent redirect for you unless you want to switch it over to temporary redirect, 302 instead of 301.

Just my two cents and soapbox here: Keep in mind if the folder /hello does not exist that is an http status code of "404 file not found" status. So, if you're intention is to do this for everything that doesn't exist then you might want to just redirect the 404 errors to your hello page otherwise scalability of making a bunch of rewrites to anticipate human behavior becomes a full-time job. Again, that's my two cents there.