Forum Moderators: phranque

Message Too Old, No Replies

Apache - Filtering URL Help

Apache - Filtering URL Help

         

ozdon

8:47 pm on Nov 16, 2006 (gmt 0)

10+ Year Member



I am having an issue with the Apache conf file.

Here is what I want to do:If a user goes to http://www.example.com/director1/index.html,
I would like the apache config file to search the url and if it finds */director1/*, I would like to redirect to a custom Error Document (thus returning a 404).

i have tried this, but it does not seem to work at all.
(1)
<Location ~ "/(director1¦director3)/*">
ReWriteEngine On
RewriteRule ^.*$ http://www.example.com/ErrorDocument
ErrorDocument 404 /ErrorDocument.html
</Location>

and 2)
I thought of another solution, I could just do
RedirectMatch ^/(director1¦director2)/.*$ /point/to/errDoc.html

But the only downside is that now the status code is a 302, and I would perfer a 404.....

Any ideas?

[edited by: encyclo at 2:12 am (utc) on Nov. 17, 2006]
[edit reason] switched to example.com [/edit]

jdMorgan

5:00 am on Nov 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



External redirects, internal rewrites, and server error responses are not at all the same thing.

Redirecting to a custom error document results in a 301 or 302 response code being sent to the client with the error document URL in the "Location" header. The client reads the header, gets the redirection URL, and requests the error document URL from the server. The server then returns the contents of the error document with a 200-OK response.

Rewriting to a custom error document simply returns the contents of the custom error document, with a 200-OK response.

I believe what you want to do is force a 404 response. If that is the case, then rewrite the request to a filepath that you are sure does not exist on your server, i.e. change your code to:


ErrorDocument 404 /error404.html
#
RewriteEngine on
#
RewriteRule ^/director[13]/ /this_path_does_not_exist.html [L]

This will take a request for the URL-path /director1/<anything> or /director3/<anything> and rewrite it to the path "/this_path_does_not_exist.html".

The result will be a file not found, which will invoke 404 error handling. The server will then send the custom error document "error404.html" as specified in the ErrorDocument directive, along with a 404-Not Found server response code.

I also show a shorter form for your "director1¦director3" pattern, though you can still use ".*" if you want to retain the <Location> container method.

Jim

ozdon

3:26 pm on Nov 17, 2006 (gmt 0)

10+ Year Member



Awesome. Thanks, that worked.