2

如何将此 PHP 代码重写为 .htaccess 规则?

$url = "$_SERVER[HTTP_HOST]";
if(strrpos($url, "m.cloudcms.co") === false){
    header("HTTP/1.0 403 Forbidden");
    echo '<h1>Forbidden Access</h1>';
    echo '<p>You have reached this page in error.</p>';
    exit;
}

这是我的.htaccess:

deny from all
allow from m.cloudcms.co
# caching static files
<IfModule mod_headers.c>
    <FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|bmp|js|css|swf|woff|svg|ttf|otf|eot)(\.gz)?$">
        Header unset Pragma
        Header unset ETag
        Header set Cache-Control "max-age=-1, no-store, no-cache, must-revalidate"
    </FilesMatch>
</IfModule>
FileETag None
<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault M-1
  ExpiresByType text/html M-1
  ExpiresByType image/gif M-1
  ExpiresByType image/jpeg M-1
  ExpiresByType image/png M-1
  ExpiresByType text/css M-1
  ExpiresByType text/javascript M-1
  ExpiresByType application/javascript M-1
  ExpiresByType application/x-javascript M-1
  ExpiresByType text/xml M-1
  ExpiresByType image/svg+xml M-1
  ExpiresByType application/x-font-ttf M-1
  ExpiresByType application/x-font-truetype M-1
  ExpiresByType application/x-font-opentype M-1
  ExpiresByType application/vnd.ms-fontobject M-1
  ExpiresByType application/x-font-woff M-1  
</IfModule>


# For servers that support output compression, you should pick up a bit of
# speed by un-commenting the following lines.

php_flag zlib.output_compression On
php_value zlib.output_compression_level 9

现在正在为 cloudcms.co 和 m.cloudcms.co 发送 403 Forbidden 消息

4

1 回答 1

2

您需要确定要匹配的 URI 是什么,然后使用文档根目录(或某个适当位置)中的规则,您可以进行相同类型的检查。

"$_SERVER[HTTP_HOST]"本质上是变量%{HTTP_HOST}。要返回 403,您可以简单地F在规则中使用标志:

RewriteCond %{HTTP_HOST} !^m.cloudcms.co$ [NC]
RewriteRule ^(.*)$ - [L,F]

在这里,被匹配的 URI 是(.*), 或all。如果您想将其缩小到更具体的内容,您需要修改正则表达式以匹配。

对于/file.php

RewriteRule ^file\.php$ - [L,F]

对于目录中的所有内容/some/path/

RewriteRule ^some/path/ - [L,F]

等等

于 2013-05-06T20:31:33.893 回答