.htaccess - Allow 3 ip's, redirect for other ip's and redirect to mobile site
问题描述
I want to allow three IPs to my site and show a message for other IPs. Also, I want to redirect to a mobile site if the site is visited with a mobile device.
This all in htaccess. I tried if statements in htaccess but for some reason they won't work.
What i need is this
if ((ip=1.2.3.4 OR ip=5.6.7.8 OR ip=9.10.11.12) AND ismobile=true)
{
redirect to mobile site
}
elseif ((ip=1.2.3.4 OR ip=5.6.7.8 OR ip=9.10.11.12) AND ismobile=false)
{
redirect to normal site
}
else
{
redirect to site with message
}
解决方案
使用您显示的示例,您可以尝试以下操作。更改yoursite
您的实际域名。另外,请确保在测试您的 URL 之前清除浏览器缓存。
<IfModule mod_rewrite.c>
RewriteEngine ON
RewriteCond %{REMOTE_ADDR} ^((1\.2\.3\.4)|(5\.6\.7\.8)|(9\.10\.11\.12))$
RewriteRule ^ http://yoursite.com/ [R=301,L]
RewriteCond %{REMOTE_ADDR} !^((1\.2\.3\.4)|(5\.6\.7\.8)|(9\.10\.11\.12))$
RewriteRule ^ message.php [L]
</IfModule>
这是上述正则表达式的在线正则表达式演示。