重写规则htaccess的文件是否存在 [英] RewriteRule htaccess if a file exists

查看:82
本文介绍了重写规则htaccess的文件是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用htaccess的,以检查是否存在于相同的目录htaccess文件名为offline.txt文件,如果存在,那么做的:

I want to use htaccess to check if a file called offline.txt exists in the same directory as the htaccess file, if it exists then do:

RewriteRule ^.*$ /websiteOffline.php [L] 

和也有一个例外,在一定范围内的IP地址

and also have an exception to a certain range of ip addresses

我们的想法是,如果我想利用我的整个网站停机产品保养我可以简单地地方叫offline.txt一个文件,这将做到这一点,并允许某些IP的绕过这个条件。

The idea is that if i want to take my entire site down for maintanance i could simply place a file called offline.txt and that would do it and allow certain ip's to bypass this condition.

在如何能够来达到的任何建议?

Any suggestions on how this can be acheived?

感谢

我已经试过以下code,但似乎没有工作:

i've tried the following code but doesnt seem to work:

RewriteEngine on
RewriteCond /offline.txt -f
RewriteRule  ^(.*)  /websiteOffline.php [L]

更新

只需使用乔恩的建议为folows:

Just used Jon's suggestion as folows:

RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/offline.txt -f
RewriteRule  ^(.*)  /websiteOffline.php [L]

轻微问题,这是该文件websiteOffline.php是其中的RewriteCond施加所以因此不能够显示该文件夹中。任何建议

Slight problem with this is that the file websiteOffline.php is in the folder where the rewritecond is applied so hence is not able to display that file. any suggestions

推荐答案

您不能用于 -f 测试的相对位置。你需要包括完整路径:

You can't use a relative location for the -f test. You'll need to include the full path:

RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/folder/offline.txt -f
RewriteCond %{REQUEST_URI} !/websiteOffline.php
RewriteCond %{REMOTE_ADDR} !^12\.34\.56\.78$
RewriteRule  ^(.*)  /websiteOffline.php [L]

其中, /文件夹/ 是你的htaccess文件是在路径中。

where /folder/ is the path that your htaccess file is in.

在这里,如果你的IP地址是 12.34.56.78 ,并尝试访问该文件夹,也不会重定向你。

Here, if your IP address is 12.34.56.78 and you try accessing the folder, it won't redirect you.

这篇关于重写规则htaccess的文件是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆