Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Is it possible to let Apache automatically return a 404 page for a single URL using the Location tag?

<Location "/some/url">
    # ???
</Location>

Solutions without mod_rewrite or any other modules preferred.

share|improve this question

migrated from superuser.com Mar 1 '11 at 18:34

This question came from our site for computer enthusiasts and power users.

2 Answers 2

up vote 5 down vote accepted

The only solution "without any modules", is to not have the resource exist. If the location doesn't exist, then it Apache will return a 404.

Otherwise, you will need to use mod_alias or mod_rewrite. mod_alias is the simpler and more efficient solution:

Redirect 404 /your/url/path

You use this inside your vhost, there is no need to put it in a Location block.

You can also use the RedirectMatch directive if you don't want to match URLs below /your/url/path:

RedirectMatch 404 ^/your/url/path$

This and more can be read in the mod_alias documentation

share|improve this answer
    
Nice idea, but unfortunately this will also return 404 for anything matching '/your/url/path/*', which is a problem for me. –  AndiDog Mar 1 '11 at 19:17
1  
@Andi: You should be more specific in your questions. –  hobodave Mar 1 '11 at 19:19
    
Sorry for that ;) The RedirectMatch solution works like a charm. –  AndiDog Mar 1 '11 at 19:29

Apparently you can use a RewriteRule for this (make sure your RewriteEngine is enabled):

RewriteRule ^/forbidden_ /nonexistent [L]

Got this information from "return 404 for specific url?" in the Apache mailing list archives.

share|improve this answer
    
Well, I'd like to have a generic non-rewrite solution. I know the mod_rewrite solution already. –  AndiDog Mar 1 '11 at 18:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.