11

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.

AndiDog
  • 321
  • 2
  • 5
  • 19

2 Answers2

17

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

hobodave
  • 2,800
  • 2
  • 23
  • 33
1

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.

random
  • 450
  • 1
  • 9
  • 16
  • Well, I'd like to have a generic non-rewrite solution. I know the mod_rewrite solution already. – AndiDog Mar 01 '11 at 18:47
  • People are so used to use `mod_rewrite` for what it was never intended for… your solution will not work if website uses `FallbackResource` directive. `Redirect 404 /path`, however, will. – AnrDaemon Mar 23 '20 at 21:50