Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure an Apache rewrite-rule to redirect all requests to 404 unless explicitly allowed

I'm searchin on internet since yesterday and i couldn't find the answer. At least one that works for apache2.

I just want a set of rewritemod rules that will make apache redirect everything to a 404 but allows me to explicitly override it for some files/folders.

I've tried an inclusive rule but i couldn't get it working.

Thanks.

like image 452
Albert Knightley Avatar asked Jun 02 '11 16:06

Albert Knightley


2 Answers

Just try this line in your .htaccess file:

RewriteRule ^(?!(allowedFile\.html|allowedDirectory/)) - [R=404,L,NC] 

Above rule will throw 404 for everything except a file called allowedFile.html and a folder called allowedDirectory.

like image 171
anubhava Avatar answered Sep 21 '22 09:09

anubhava


RewriteRule ^/protectedfolder/(.*) -  [L] /* no substitution , serve as it is for this folder*/
RewriteRule ^$ - [R=404,L] /*reject everything */
like image 20
Rizwan Sharif Avatar answered Sep 22 '22 09:09

Rizwan Sharif