重定向或拒绝直接访问公用文件夹

时间:2015-05-18 02:09:55

标签: apache .htaccess mod-rewrite

我使用此htaccess直接访问public/csspublic/jspublic/images个文件夹,重写从url中移除public:

<IfModule mod_rewrite.c>
    RewriteEngine On

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

EG。访问此网址http://localhost/css/file.css时显示http://localhost/public/css/file.css

我想拒绝访问网址http://localhost/public/css/file.css,但允许http://localhost/css/file.css,可以这样做吗?

1 个答案:

答案 0 :(得分:0)

要解决此问题,您可以将%{THE_REQUEST}RewriteCond一起使用,例如:

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

如果需要任何方法(GETPOSTPUT等),请重定向,例如:

<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>

拒绝访问例如:

<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>