2012-08-28 104 views
1

我的CakePHP 2网站有以下.htaccess文件。
我把它放在webroot文件夹下。CakePHP站点使用.htaccess限制对某些文件的访问

每天一些机器人尝试登录到我的网站作为WordPress的网站。所以我需要限制一些文件名(wp-login.php)或者一些目录如Administrator或者cache。

  • 但是,当我进入到example.com/wp-login.php我得到“错误:发生内部错误。” CakePHP异常的页面。
  • 当我取消注释“目录/管理员”或“目录/缓存”每个页面给出403错误。

我该如何限制到那些文件和文件夹?

<FilesMatch "\.(htaccess|htpasswd|ini|phps|fla|psd|log|sh)$"> 
Order allow,Deny 
Deny from all 
</FilesMatch> 

<Files wp-login.php> 
    Order allow,deny 
    Deny from all 
</Files> 

#<Directory /Administrator> 
# Order allow,deny 
# Deny from all 
#<Directory> 

#<Directory /Cache> 
# Order allow,deny 
# Deny from all 
#<Directory> 


<IfModule mod_rewrite.c> 
    RewriteEngine On 

    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] 
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L] 

    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteRule ^(.*)$ index.php [QSA,L] 
</IfModule> 

#set file cache maximum age in seconds 
<ifmodule mod_headers.c> 
    <filesmatch "\.(ico|flv|jpg|jpeg|png|gif|css|swf)$"> 
     Header set Cache-Control "max-age=518400, public" 
    </filesmatch> 
    <filesmatch "\.(js|css)$"> 
     Header set Cache-Control "max-age=604800, public" 
    </filesmatch> 
</ifmodule> 


# gzip files 
<ifModule mod_gzip.c> 
    mod_gzip_on Yes 
    mod_gzip_dechunk Yes 
    mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$ 
    mod_gzip_item_include handler ^cgi-script$ 
    mod_gzip_item_include mime ^text/.* 
    mod_gzip_item_include mime ^application/x-javascript.* 
    mod_gzip_item_include mime ^application/javascript.* 
    mod_gzip_item_exclude mime ^image/.* 
    mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.* 
</ifModule> 

# gzip files 
<ifModule mod_deflate.c> 
    <filesMatch "\.(css|js|x?html?|php)$"> 
    SetOutputFilter DEFLATE 
    </filesMatch> 
</ifModule> 

编辑:我改变了重定向代码。目录问题解决了,但CakePHP风格的Interval服务器错误异常仍然存在。

<FilesMatch "\.(htaccess|htpasswd|ini|phps|fla|psd|log|sh)$"> 
Order allow,Deny 
Deny from all 
</FilesMatch> 

<Files "wp-login.php"> 
    Order allow,deny 
    Deny from all 
</Files> 

<IfModule mod_rewrite.c> 
    RewriteEngine On 

    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] 
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L] 

    RewriteRule ^/?(administrator|cache|undefined) - [L,F,NC] 

    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteRule ^(.*)$ index.php [QSA,L] 
</IfModule> 

回答

3

您不能在htaccess文件中使用<Directory>块。我不知道为什么你得到一个500错误的WP-login.php中,但你需要一些报价添加至宣言:

<Files "wp-login.php"> 
    Order allow,deny 
    Deny from all 
</Files> 

您可以将单独的htaccess中的AdministratorCache目录是只是:

Order allow,deny 
Deny from all 

或者你可以使用像一个重写规则:

RewriteRule ^/?(Administrator|Cache) - [L,F] 
+0

编辑的问题。目录问题通过您的评论解决。虽然我把文件名放在引号内,但wp-login错误仍然存​​在。 – trante

+1

@trante仍然没有看到任何错误。那么,不管是什么情况,你总是可以将wp-login.php添加到列表中:'RewriteRule^/?(administrator | cache | undefined | wp-login \ .php) - [L,F,NC]' –

+0

Your上次评论解决了我的问题。所以我接受:)) – trante

相关问题