根据URL而不是.htaccess中的IP地址进行重定向

时间:2019-02-13 12:27:00

标签: .htaccess url redirect

我使用.htaccess中的重定向将请求domain.com/admin的任何用户重定向到domain.com,如下所示:

RewriteCond %{REMOTE_ADDR} !^101.101.101.101
RewriteCond %{REQUEST_URI} admin [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/ [R=302,L]

这会将所有来自虚构IP地址101.101.101.101的访客重定向到其他访客,并且工作正常。

我希望能够用abc.domain.com之类的URL替换IP地址,因为IP地址101.101.101.101会随时间变化,并且已在abc.domain.com上设置了动态DNS来跟踪更新的IP地址。这可能吗?

我尝试将URL放在单引号,双引号等中无效。

2 个答案:

答案 0 :(得分:0)

您是否尝试过以下方法:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.from_domain\.com [NC]
RewriteRule ^(.*)$ http://www.to_domain.com/$1 [R=302,L]

答案 1 :(得分:0)

替换此行:

SELECT t1.category AS lev1, t2.category as lev2, t3.category as lev3, t4.category as lev4
FROM categories t1 JOIN
     categories t2
     ON t2.parentid = t1.catid JOIN
     categories t3
     ON t3.parentid = t2.catid JOIN
     categorie t4
     ON t4.parentid = t3.catid
WHERE t1.parentid = 0 AND t1.cat_type = 'content'
UNION ALL
SELECT t1.category AS lev1, t2.category as lev2, t3.category as lev3, NULL as lev4
FROM categories t1 JOIN
     categories t2
     ON t2.parentid = t1.catid JOIN
     categories t3
     ON t3.parentid = t2.catid
WHERE t1.parentid = 0 AND t1.cat_type = 'content'
UNION ALL
SELECT t1.category AS lev1, t2.category as lev2, NULL as lev3, NULL as lev4
FROM categories t1 JOIN
     categories t2
     ON t2.parentid = t1.catid 
WHERE t1.parentid = 0 AND t1.cat_type = 'content'
UNION ALL
SELECT t1.category AS lev1, NULL as lev2, NULL as lev3, NULL as lev4
FROM categories t1
WHERE t1.parentid = 0 AND t1.cat_type = 'content';

与此:

RewriteCond %{REMOTE_ADDR} !^101.101.101.101

清除浏览器缓存,然后测试,如果可以,请将RewriteCond %{HTTP_HOST} !^(www\.)?abc\.domain\.com$ 替换为R=302进行永久重定向。