检测服务器是否通过.htaccess文件重定向

时间:2012-09-19 15:30:46

标签: .htaccess

我创建了一个.htaccess文件来将特定的IP地址重定向到另一个文件:

# Allow rewriting of htaccess
RewriteEngine On
RewriteBase /

# Get remote address/block of addresses to redirect
RewriteCond %{REMOTE_ADDR} 127\.0\.0\.1

# Define the file being accessed
RewriteCond %{REQUEST_URI} /index\.php$

# When the remote address(es) try to access the file above redirect them to the file below
RewriteRule .* /other-index\.php [R,L]

这很好用,如果它尝试访问index.php但允许所有其他远程主机访问index.php,上面的方法会将localhost重定向到other-index.php。

远程主持人正在呼叫:http://www.example.com/index.php

我的问题是远程主机(在本例中为localhost)是否可以发现服务器正在重定向?

1 个答案:

答案 0 :(得分:1)

  

我的问题是远程主机(在本例中为localhost)是否可以发现服务器正在重定向?

是的,因为客户端(在127.0.0.1)向服务器发出/index.php请求。服务器响应,302将客户端重定向到其他位置/other-index.php

通常情况下,这一切都会无所事事,就像您使用浏览器一样。浏览器获取新位置(/other-index.php)并简单地向服务器发出新位置的新请求并获取它。它立即发生。但是远程主机(客户端)知道它被重定向,因为它所做的第一个请求(对于/index.php)导致302带有新位置/other-index.php)。 / p>

如果R的方括号中的RewriteRule标记被省略,则不会有重定向,并且URI将由服务器和客户端(内部重写)进行内部重写远程主持人)会忘记在other-index.php请求other.php时提供内容的事实。