简单(&A);快速但不是100%安全的方法:
此方法不是100%安全的,但对于大多数用户(如99%),它会起作用。
这里,基本上你将使用.htaccess 文件inside the restricted directory,说:/var/www/html/link 使用以下代码:
SetEnvIf Cookie "^.*wordpress_logged_in.*$" WP_LOGGED_IN=maybe
Order allow,deny
Require all granted
# Our Network
#Allow from ip-address/22
#Allow from ip-address/24
#Allow from ip-address/24
#Allow from ip-address/24
# and even longer list of IPs
# Perhaps WordPress is logged in according to cookie: works for trivial cases, but not 100% secure
Allow from env=WP_LOGGED_IN
因此,基本上,您将在此处放置允许的IP网络,而不是apache配置
.htaccess 文件(无
<Directory> 块),还将使用检查WordPress登录cookie
SetEnvIf 指令。如果IP不在匹配项中(&M);也找不到登录cookie,使用此代码,其余访问者将被拒绝访问。
在.htaccess 根web目录的文件是必需的。
安全方法:
如果您需要100%安全的方法,那么最好使用PHP。
首先从apache配置文件中删除所有现有的IP限制(对应目录)。
然后,在中使用以下代码.htaccess 根web目录的文件。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^link/access\\.php$ - [L]
RewriteRule ^link/.*$ /link/access.php [L]
# Your remaining rewrite rules for WordPress
</IfModule>
然后在
/var/www/html/link/access.php 文件中,请使用以下PHP代码:
<?php
function network_match( $ip, $networks ) {
foreach( $networks as $network ) {
$net_parts = explode( \'/\', $network );
$net = $net_parts[0];
if( count( $net_parts ) === 2 ) {
$netmask = $net_parts[1];
if( ( ip2long( $net ) & ~ ( ( 1 << ( 32 - $netmask ) ) - 1 ) ) === ( ip2long( $ip ) & ~ ( ( 1 << ( 32 - $netmask ) ) - 1 ) ) ) {
return true;
}
}
else if( $ip === $net ) {
return true;
}
}
return false;
}
$networks = array(
// Put your allowed networks HERE
"192.168.0.0/24",
"127.0.0.1"
);
$allowed = false;
if( network_match( $_SERVER[\'REMOTE_ADDR\'], $networks ) ) {
$allowed = true;
}
else {
// assuming WordPress is installed one directory above
// if not, then change accordingly
require_once dirname( dirname( __FILE__ ) ) . "/wp-load.php";
if( is_user_logged_in() ) {
$allowed = true;
}
}
if( $allowed ) {
// HERE you SERVE THE REQUESTED FILE WITH PHP
echo "You are allowed";
}
else {
die("Access denied!");
}
正如现在一样,这段代码基本上在内部将所有请求重写为
/var/www/html/link/ 目录收件人
/var/www/html/link/access.php &文件;它检查IP访问权限;WordPress登录。
您可以修改此PHP代码(在以下行中):// HERE you SERVE THE REQUESTED FILE WITH PHP) 从link 目录
您可以查看此帖子server files from PHP CODE.