2015-05-18 37 views
0

我使用這個htaccess的訪問public/csspublic/jspublic/images文件夾直接,它重寫通過網址刪除公衆拒絕公用文件夾的直接訪問:重定向或

<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

我想拒絕訪問的URL http://localhost/public/css/file.css但允許http://localhost/css/file.css,就可以做到這一點?

+0

我可以問爲什麼這很重要?無論哪種方式,你仍然可以訪問css文件。無論如何,這在源頭上。所有的資產都將提供給客戶。 –

+0

@PanamaJack感謝您的關注,沒有特別的理由,這將是一個「規範URL」的問題。 –

+0

我在想的問題是,用戶永遠不會看到網址,並且可以使用robots.txt來防止抓取該文件夾,但是我只是沒有看到資產文件夾上必須可用於頁面的點正確顯示。 –

回答

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>