Search code examples
apache.htaccessmod-rewriteurl-rewritingfriendly-url

.htaccess deny access to all except one specific file


I have this rule in my root .htaccess file:

# DENY ACCESS TO EVERYTHING INSIDE PHP DIRECTORY
RewriteCond %{HTTP_REFERER} !^http://(www\.)?someurl [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?someurl.*$ [NC]
RewriteRule ^php - [F]

It denies access to all files inside the "php" directory if accessed outside my server.

How to add one specific file exception, let's call that file "_sharer-socnets.php" using above logic of RewriteCond / RewriteRule?


Solution

  • To deny all but one file you can use this rule:

    # DENY ACCESS TO EVERYTHING INSIDE PHP DIRECTORY
    RewriteCond %{HTTP_REFERER} !^http://(www\.)?someurl [NC]
    RewriteCond %{HTTP_REFERER} !^http://(www\.)?someurl.*$ [NC]
    RewriteCond %{REQUEST_URI} !/_sharer-socnets\.php$ [NC]
    RewriteRule ^php - [F,NC]