Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess to redirect to 404 (Not Found) message on unauthorized folder access

I want to display a 404 (Not found) message, when the user tries to access unauthorized directory. I do not want to use Forbidden pages. I have tried with below code, but it does not work.

RedirectMatch 404 ^/htaccess/.*$

Any idea on what might be going wrong?

like image 692
Leon Avatar asked Jan 07 '12 01:01

Leon


People also ask

Where is .htaccess located?

htaccess file is located in the root directory of your WordPress site. Depending on your hosting provider, the root directory may be a folder labelled public_html, www, htdocs, or httpdocs. You can locate it by using File Manager in your hosting account's cpanel. Let's walk through the process step-by-step.


1 Answers

try adding the following to your htaccess file

#if the URI starts with htaccess, return a 404
RewriteRule ^htaccess/ - [R=404,L,NC]

Edit

eg system/function,system/class,...

If you have a lot of folders then use a RewriteCond as below as it is easier to read than multiple folders in a single RewriteRule or multiple RewriteRules, which duplicate the 404 piece

RewriteCond %{REQUEST_URI} ^htaccess/ [NC,OR]
RewriteCond %{REQUEST_URI} ^system/function/ [NC,OR]
#just make sure the last rule does  NOT have an OR
RewriteCond %{REQUEST_URI} ^system/class/ [NC]
RewriteRule . - [R=404,L,NC]
like image 101
Ulrich Palha Avatar answered Oct 05 '22 17:10

Ulrich Palha