Google Answers Logo
View Question
 
Q: Apache Configuration ( Answered,   2 Comments )
Question  
Subject: Apache Configuration
Category: Computers > Internet
Asked by: riegel-ga
List Price: $8.00
Posted: 23 May 2002 07:55 PDT
Expires: 30 May 2002 07:55 PDT
Question ID: 17659
I would like to configure my apache server to do the following.

When someone types in:
http://www.positiveaction.org/yc251
this page currently returns 404 Error.

Can I configure Apache to redirect to:
http://www.positiveaction.org/pages/start/newsite/products.html?product=yc251

and have it work for any 404 Page, then I can program my product page
to display an error if the product/page doesn't exist.

I only want to know how to have apache rewrite the URL for 404 pages.

Following is some extra information that may or may not be helpful:
We have a product database on our server that people can go to our
index page and search. I want to create a simple url for them to
remember that would allow them to find a specific product. For Example
we have a Bible Study called "She Said Yes" that we sell. If I want to
direct someone to that study they would use the following URL:
www.positiveaction.org/yc251. This page right now is a 404 Error.
Answer  
Subject: Re: Apache Configuration
Answered By: daveslipp-ga on 23 May 2002 08:36 PDT
 
Hi;

This is something that comes up more often than you'd expect, and lots
of people are in the dark about it, too, as all of those generic 404
pages show.

To do the kind of "intelligent" error handling you want, you need a
two step solution.

The first step is to edit your .htaccess file. You can find it at the
root of your site. If there is a an "ErrorDocument" directive in that
file, you  need to change it. If there none, you have to add it. Full
documentation of this directive is in the Apache documentation at

http://httpd.apache.org/docs-2.0/mod/core.html#errordocument .

In most cases you'd add something like this:

ErrorDocument 404 /notfound.html

This would over-ride the generic message and send any missing page
urls to a page called "notfound.html" so that you could at least have
a page that matched the rest of your site and gave the user a few
options such as links to go to the site home page, search page or
whatever.

For the second step, to get "intelligent" error handling, the easiest
way to make it work is to point that ErrorDocument directive to a
script page. Depending on what scripting languages your server
configuration supports, you'd write a script that would capture parts
of the request url, and then redirects the user to a different url
based on what it captured.

For example, in PHP you'd have something like this embedded in the
page:

<?php


$theSearchString = getenv("REQUEST_URI");
Header("Location: http://www.thedomain.com/dbsearch.php?item=$theSearchString";

?>

This script would :
a) grab the part of the url after the domain slash and call it
theSearchString, and
b) redirect the browser to  a new url, made up of the path to the
database search page and theSearchString .

(Note - In the real world, you'd probably will want to add some code
into this to make sure that you were only passing item numbers to that
search page, not merely misspelled page names. You would probably do
best to specify different processing for bad urls including the string
.html for example.)

In your situation, it appears that the .htaccess thing has been done,
and that there's a page already in place that is trying to do exactly
what I've just described. The error page shows the appropriate catalog
entry, but just adds an inappropriate page expired warning. Have a
look through the code of your error handling page, and I'll bet you'll
find a subtle error - like a case that is too broadly defined - in the
expired page module.

I hope this had been helpful, and that it hasn't been too tedious (but
you know how it goes when a geek gets rolling...)

All the best;
daveslipp-ga

Request for Answer Clarification by riegel-ga on 23 May 2002 11:11 PDT
Thanks for the answer. So it sounds like you are telling me there is
no way to do this with Apache. I want to redirect as I have specified
above. Not as you have specified in your answer.

If you had taken time to look at my site you would have noticed that I
am already doing inteligent error handling. My question is how can I
embed the page request in the redirected url at the web server level.
If you tell me this is not possible, and someone reading this knows of
a way to do it please let me know. I would like to do it at the Apache
level.

I don't think you have answered this.

Clarification of Answer by daveslipp-ga on 23 May 2002 11:28 PDT
Sorry for the miscommunication.

What I'm saying is that your server is already doing what you
describe, except there seems to be a minor misconfiguration in the
script you have running.

When I click on the second link, I see the proper product entry, but
there is a warning icon above the item information, and in large red
letters there is a warning that  says "The link you are looking for
has expired. In most cases this will not affect your ability to use
our site."

The url I am redirected to when I follow the second link above is as
follows:

http://www.positiveaction.org/pages/start/newsite/index.html?tag=404

If you check in the script file and fix that error, everything should
work as you describe. I'f you'd like a screen shot of what I'm geting,
please let me know.

Request for Answer Clarification by riegel-ga on 23 May 2002 14:27 PDT
I don't think you are answering my question. I know how to do this
using a scripting language (in my case HTML/OS).

Answer this question only:
When someone types in: 
http://www.positiveaction.org/yc251 
Can I configure Apache to redirect to: 
http://www.positiveaction.org/pages/start/newsite/products.html?product=yc251
where "yc251" could be anything.

Clarification of Answer by daveslipp-ga on 23 May 2002 15:33 PDT
Are you trying to accomplish the same result without a script? i.e.
striclty within Apache?

If so, you can do it, but you'd need to write, build and install a
custom module in Apache to do it. The built in processing in Apache
isn't robust enough on its own to do the error checking that you'll
require.

I've seen people do this type of custom module porduction, but
normally they do it on a dare, to show off, or as part of a job where
they're being paid by the hour, since it takes so much longer to do
the module than the scripting solution.
Comments  
Subject: Re: Apache Configuration
From: wildeeo-ga on 23 May 2002 15:04 PDT
 
Hi,

It's possible to do that using the mod_rewrite feature of apache
(assuming it's enabled on your server). PHP.net does something like
this - when you type in an address like http://php.net/keyword, it
goes and looks up 'keyword'. They've put a howto up at
http://www.php.net/urlhowto.php.

If you want more info on mod_rewrite, check out the tutorial at 
http://fantomaster.com/faarticles/modrewrite01.txt,
http://fantomaster.com/faarticles/modrewrite02.txt and, surprisingly,
http://fantomaster.com/faarticles/modrewrite03.txt.

--wildeeo
Subject: Re: Apache Configuration
From: daveslipp-ga on 23 May 2002 15:44 PDT
 
Good point...rewrite MIGHT do it, though the learning curve on the job
would be devastating. Again, it'd be one of those things you'd do on a
dare or by the hour. I'd love to see it.

For light bedtime reading, here's the documentation:

http://httpd.apache.org/docs-2.0/mod/mod_rewrite.html

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy