Tuesday, January 4, 2022

[SOLVED] Forbid access to directory, but not alias?

Issue

Using Apache. How can I allow access to /storage/ but deny access to /.storage/ when /storage/ is an alias for /.storage/subdomain/?

# Define alias based on subdomain
RewriteCond %{HTTP_HOST} ^([^.]+)$
RewriteRule ^storage/(.*)$ ".storage/%1/$1" [QSA,L]

# Forbid access to 
RewriteCond %{REQUEST_URI} ^/\.storage/
RewriteRule ^ - [F,L]

Solution

Based on your shown samples, could you please try following. Please make sure you clear your browser cache before testing your URLs. We need to fix regex for getting sub-domain first then we need to change " from rules and add an additional condition to checks to make sure users are not able to access actual folder directly.

RewriteEngine ON
# Define alias based on subdomain
RewriteCond %{HTTP_HOST} ^(?:www\.)(.*)$ [NC]
RewriteRule ^storage/(.*)$ .storage/%1/$1 [NC,QSA,L]

# Forbid access to 
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^\.storage - [F,NC,L]


Answered By - RavinderSingh13