编写Apache配置文件,实现不同IP客户端访问指定目录

      发布在:后端技术      评论:0 条评论

编写Apache配置文件,客户端IP119.128.128.28访问/www/wwwroot/80zx.com目录。其他IP访问/www/wwwroot/80zx.cn目录。这里还考虑到其他ip访问条件,具体代码

RewriteEngine On
#第一个IP条件
RewriteCond %{REMOTE_ADDR} ^119\.128\.128\.28$
RewriteRule ^/(.*)$ /www/wwwroot/80zx.com/$1 [L]
#第二个IP条件
RewriteCond %{REMOTE_ADDR} ^182\.198\.18\.(.*)$
RewriteRule ^/(.*)$ /www/wwwroot/80zx.com/$1 [L]
#第3个IP条件
RewriteCond %{REMOTE_ADDR} ^119\.183\.8\.(.*)$
RewriteRule ^/(.*)$ /www/wwwroot/80zx.com/$1 [L]

实际Apache配置文件参考

<VirtualHost *:80>
ServerAdmin webmaster@example.com
DocumentRoot "/www/wwwroot/80zx.cn"
ServerName abc.80zx.cn
ServerAlias 80zx.cn www.80zx.cn
#errorDocument 404 /404.html
ErrorLog "/www/wwwlogs/80zx.cn-error_log"
CustomLog "/www/wwwlogs/80zx.cn-access_log" combined


#重点开始
RewriteEngine On
# 检查是否为手机端访问
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule ^/(.*)$ /www/wwwroot/mobile/$1 [L]
#第一个IP条件
RewriteCond %{REMOTE_ADDR} ^119\.128\.128\.28$
RewriteRule ^/(.*)$ /www/wwwroot/80zx.com/$1 [L]
#第二个IP条件
RewriteCond %{REMOTE_ADDR} ^182\.198\.18\.(.*)$
RewriteRule ^/(.*)$ /www/wwwroot/80zx.com/$1 [L]
#第3个IP条件
RewriteCond %{REMOTE_ADDR} ^119\.183\.8\.(.*)$
RewriteRule ^/(.*)$ /www/wwwroot/80zx.com/$1 [L]
#重点结束


#DENY FILES
<Files ~ (\.user.ini|\.htaccess|\.git|\.env|\.svn|\.project|LICENSE|README.md)$>
Order allow,deny
Deny from all
</Files>

#PHP
<FilesMatch \.php$>
SetHandler "proxy:unix:/tmp/php-cgi-00.sock|fcgi://localhost"
</FilesMatch>


#PATH
<Directory "/www/wwwroot/80zx.cn">
SetOutputFilter DEFLATE
Options FollowSymLinks
AllowOverride All
Require all granted
DirectoryIndex index.php index.html index.htm default.php default.html default.htm
</Directory>
<Directory "/www/wwwroot/80zx.com">
SetOutputFilter DEFLATE
Options FollowSymLinks
AllowOverride All
Require all granted
DirectoryIndex index.php index.html index.htm default.php default.html default.htm
</Directory>
<Directory "/www/wwwroot/mobile">
SetOutputFilter DEFLATE
Options FollowSymLinks
AllowOverride All
Require all granted
DirectoryIndex index.php index.html index.htm default.php default.html default.htm
</Directory>
<Directory "/www/wwwroot/pc">
SetOutputFilter DEFLATE
Options FollowSymLinks
AllowOverride All
Require all granted
DirectoryIndex index.php index.html index.htm default.php default.html default.htm
</Directory>

</VirtualHost>


热门推荐