0

How to allow to view a file in (/../)subfolder using .htaccess ?

Folder structure:

root/
root/.htaccess
root/public
root/public/.htaccess
root/public/index.php
root/public/css
root/public/js

root/.htaccess

 RewriteEngine On
 RewriteBase /
 Options -MultiViews
<FilesMatch  ".*">
  Deny from all
</FilesMatch>

#i tried to add here, public directory permission, but it does not work
#with DirectoryMatch i am getting the error The server encountered an internal error or misconfiguration and was unable to complete your request.
#without DirectoryMatch i am getting the error You don't have permission to access /type705b/public/index.php on this server.
<DirectoryMatch "/public.*">
 Allow from All
</DirectoryMatch>   

root/public/.htaccess

RewriteEngine On
RewriteBase /public/
Options -MultiViews
#wrong according comments <FilesMatch "public.*" >
  Allow from all
#</FilesMatch>
#the rest of htaccess below

If i try to view root/public/index.php, i am getting the error :Forbidden You don't have permission to access /root/public/index.php on this server.

After the corrections, i am getting The server encountered an internal error or misconfiguration and was unable to complete your request. The error log for the code above says xxx/htdocs/root/.htaccess: <DirectoryMatch not allowed here

Gintare Statkute
  • 677
  • 2
  • 8
  • 18
  • Possible duplicate of [.htaccess. deny root, allow specific subfolder. Possible?](https://stackoverflow.com/questions/7649794/htaccess-deny-root-allow-specific-subfolder-possible) – matiaslauriti May 24 '17 at 15:03

3 Answers3

2

Since you don't have any rewrite rule in public/.htaccess, you don't really need anything in public/.htaccess other than this line:

Allow from all

Moreover Files directive is used for matching file names only so it can never match a file path that includes directories also.

anubhava
  • 761,203
  • 64
  • 569
  • 643
1

If I am not wrong, you have <Files ~ "^public.*" >, that's wrong.

The correct one should be <Files ~ ".*" > because you already has the RewriteBase /public/


<Files ~ "^public.*" > will allow only files that match public.* but as a file name, not as a path nor dir.

matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
0

The working combination to allow to view a file in (/../)subfolder using .htaccess :

Folder structure:

root/
root/.htaccess
root/public
root/public/.htaccess
root/public/index.php
root/public/css
root/public/js

root/.htaccess

RewriteEngine On
RewriteBase /
Options -MultiViews
Deny from all

root/public/.htaccess

RewriteEngine On
RewriteBase /public/

Options -MultiViews
Allow from all
#the rest of code

Using <File > <FileMatch > <Directory > <DirectoryMatch > was giving the errors.

Gintare Statkute
  • 677
  • 2
  • 8
  • 18
  • That's exactly what I wrote in my answer. You don't even need `RewriteEngine` and `RewriteBase` lines in both .htaccess – anubhava May 24 '17 at 15:18