我在我的网站上安装了另一个主题,我需要改进它。 我怎样才能更改索引文件只为我的IP地址?
我当前的htaccess文件:
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP_HOST} ^example.com RewriteRule (.*) http://www.example.com/$1 [R=301,L] RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule>
我需要将索引文件更改为index.WP.php
为IP地址203.0.113.111
。 可能吗?
您可以重复前端控制器部分,并为您的IP地址添加条件。 例如:
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} ^example\.com [NC] RewriteRule (.*) http://www.example.com/$1 [R=301,L] ### ALTERNATIVE FRONT CONTROLLER FOR SPECIFIC IP ADDRESS RewriteRule ^index\.WP\.php$ - [L] # Special case for document root to override DirectoryIndex RewriteCond %{REMOTE_ADDR} =203.0.113.111 RewriteRule ^$ /index.WP.php [L] # Route all other requests for specific IP address RewriteCond %{REMOTE_ADDR} =203.0.113.111 RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.WP.php [L] ### DEFAULT FRONT CONTROLLER FOR ALL OTHER USERS RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule>
这只会将IP地址为index.WP.php
的用户路由到index.WP.php
。 所有其他访问者都会通过默认的前端控制器。 这仍然允许任何用户直接请求index.WP.php
如果他们想(如果他们知道它存在)。
更新:请注意,我已经改变了第一个前端控制器规则块的 RewriteRule
模式 .
到 ^
– 这允许它匹配文档根,即。http://www.example.com/
,因此它可以覆盖默认的DirectoryIndex
。
更新:对于“替代”前端控制器(通过特定的IP地址),为了覆盖DirectoryIndex
(即index.php
),文档根目录需要特殊情况。 (以前的更新是不够的,因为文档根目录显然是一个目录,所以前面的条件 ( !-d
)仍然失败。)
RewriteRule (.*) http;//www.example.com/$1 [R=301,L]
请注意,在您的规范非www到wwwredirect的scheme之后,您有一个错误的分号( ;
)。
你可以把下面的代码放在你的index.php
文件中。 然后将203.0.113.111
更改为您的公共/静态IP并更改位置URL。 这将解决您的关注
if($_SERVER['REMOTE_ADDR']=='203.0.113.111' || $_SERVER['HTTP_X_FORWARDED_FOR']=='203.0.113.111') { header("Location: http://www.example.com/yourindex.php"); } else { header("Location: http://www.example.com/index.php"); }