Send a 404 error via htaccess?
Asked Answered
D

4

33

I'd like to cut off access to a subdirectory on my site but I want any access in the subdirectory to be a 404 error, not a 403 forbidden. How can this be accomplished?

Dinerman answered 11/9, 2013 at 0:5 Comment(1)
redirect to a non-existent directory should do it... use a rewrite ruleTeen
R
84

Try:

RewriteRule ^directory/ - [L,R=404]

This redirects all requests for the folder "/directory/", they get a 404 response.

Relentless answered 11/9, 2013 at 0:10 Comment(6)
along with this 404 status, can we show the content of a 404.html page?Shorthanded
what about PHP and css/html, can they still access the folder?Identical
I get 500 in log instead 404Complicated
do you have mod_rewrite turned on?Relentless
RewriteEngine On RewriteCond %{REQUEST_METHOD} GET RewriteRule ^(.*)$ - [L,R=404] if you want all subdirectories and files inside the folder included with the GET method only.Downturn
mod_rewrite.c have to be enabledSilicium
C
5

I think, 410 error better

RewriteRule ^directory/ - [L,R=410]

or my search:

RewriteCond %{HTTP_USER_AGENT} (googlebot|bingbot|Baiduspider) [NC]
RewriteRule .* - [R=410,L]

on russian wiki

404 Not Found [19] - the most common mistake when using the Internet, the main reason - an error in writing the address of a Web page. The server understood the request, but did not find the corresponding resource at the specified URL. If the server knows that there was a document at this address, then it is desirable for it to use code 410. Answer 404 can be used instead of 403 if you need to carefully hide certain resources from prying eyes. Appeared in HTTP/1.0.

Complicated answered 16/4, 2019 at 11:1 Comment(0)
A
3
Redirect 404

Put this into the .htaccess file in the directory that you want off the record. The Redirect directive is part of mod_alias and can be used to send any status code, not just redirects.

Acapulco answered 11/8, 2022 at 17:5 Comment(0)
J
2

You could also for the time being change which page a user will see when confronted with a 403 error, but I wouldn't recommend doing this long-term.

.htaccess:

ErrorDocument 403 /your404pagehere.php
Jug answered 11/9, 2013 at 0:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.