Redirect or deny direct access to public folder

2

I'm using htaccess to access the css , js , images folders that are in /public directly:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteBase /
    RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [QSA,L]
</IfModule>

For example, when access is url http://localhost/css/file.css it shows the contents of http://localhost/public/css/file.css

But I want to deny access if the user types "public" in the address, for example http://localhost/public/css/file.css . Is it possible to deny access to /public in the address?

    
asked by anonymous 18.05.2015 / 04:45

2 answers

1

To solve the problem you can use %{THE_REQUEST} with RewriteCond , example:

RewriteCond "%{THE_REQUEST}" "^GET\s/public/"

If you need all methods ( GET , POST , PUT , etc), example redirection:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteBase /

    RewriteCond "%{THE_REQUEST}" "^[A-Z]+\s/public/"
    RewriteRule ^public/(.*)$ other-directory/$0 [QSA,L]

    RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [QSA,L]
</IfModule>

Deny access (you will need to create a false path):

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteBase /

    RewriteCond "%{THE_REQUEST}" "^[A-Z]+\s/public/"
    RewriteRule ^public/(.*)$ fake-directory/$0 [F]

    RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [QSA,L]
</IfModule>
    
18.05.2015 / 16:03
1

Since the RewriteBase directive is the root of http://localhost/ , add the following rule:

RewriteRule ^(public/) - [F,L,NC]

This rule will deny access to the public / folder. If you want to add some other specific path, see an example

RewriteRule ^(public/|outrapasta/) - [F,L,NC]

In this example, you are applying the rule to public / and another folder /.

    
18.05.2015 / 04:55