Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果存在install.php,则访问install.php,否则访问index.php_Php_Regex_Apache_.htaccess_Mod Rewrite - Fatal编程技术网

如果存在install.php,则访问install.php,否则访问index.php

如果存在install.php,则访问install.php,否则访问index.php,php,regex,apache,.htaccess,mod-rewrite,Php,Regex,Apache,.htaccess,Mod Rewrite,在访问我的php脚本之前,用户需要访问install.php并配置脚本。install.php将在完成后删除自身。删除install.php后,用户必须能够访问index.php 我所尝试的: RewriteEngine On RewriteBase /myscript/ RewriteRule ^ install.php [E] 如果install.php不存在,我可以将用户重定向到install.php,但不能重定向到index.php 我不知道检查install.php是否存在的重写条件

在访问我的php脚本之前,用户需要访问install.php并配置脚本。install.php将在完成后删除自身。删除install.php后,用户必须能够访问index.php

我所尝试的:

RewriteEngine On
RewriteBase /myscript/
RewriteRule ^ install.php [E]
如果install.php不存在,我可以将用户重定向到install.php,但不能重定向到index.php

我不知道检查install.php是否存在的重写条件:

RewriteEngine On
RewriteBase /myscript/
RewriteCond ?? ??
RewriteRule ^ install.php [E]
感谢您的帮助。顺便说一句,我一点也不擅长.htaccess配置

更新:

感谢您的评论和回复。是的,我知道我可以通过我的php脚本重定向,如:

if(file_exists('install.php')){
     header('Location: http://'.$domain.'/install.php');
}

但是我们可以对.htaccess执行同样的操作吗?

我会尝试使用该方法检查文件是否存在

bool file_exists ( string $filename )

我会尝试使用该方法检查文件是否存在

bool file_exists ( string $filename )

对于纯
.htaccess
解决方案:

RewriteCond %{DOCUMENT_ROOT}/myscript/install.php -f
RewriteRule ^ /myscript/install.php [L]

对于纯
.htaccess
解决方案:

RewriteCond %{DOCUMENT_ROOT}/myscript/install.php -f
RewriteRule ^ /myscript/install.php [L]

尝试将其添加到
myscript
文件夹中的
.htaccess
文件中:

RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/myscript/install.php -f
RewriteRule ^ install.php [L]
解释

  • 该条件检查文件
    install.php
    是否存在于
    myscript
    文件夹中
  • 如果满足条件,则规则将把任何请求(因为字符串锚点的开头
    ^
    将匹配任何文件)重写为
    install.php

尝试将其添加到
myscript
文件夹中的
.htaccess
文件中:

RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/myscript/install.php -f
RewriteRule ^ install.php [L]
解释

  • 该条件检查文件
    install.php
    是否存在于
    myscript
    文件夹中
  • 如果满足条件,则规则将把任何请求(因为字符串锚点的开头
    ^
    将匹配任何文件)重写为
    install.php

您可以使用
DirectoryIndex
实现您想要的()。在应用程序根目录中的
.htaccess
中保存此文件

DirectoryIndex install.php index.php

RewriteEngine on
RewriteBase /myscript/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewrietRule . index.php [E]

如果用户转到安装应用程序的目录,例如
http://example.com/myscript/
,它将首先尝试加载install.php,然后如果不存在,它将尝试加载index.php。如果用户转到
http://example.com/myscript/index.php
,您运气不好,但转到该url毫无意义!如果您担心这个问题,最好使用romaneso的答案,始终加载index.php,并在需要时从那里加载install.php。

您可以使用
DirectoryIndex
来实现所需()。在应用程序根目录中的
.htaccess
中保存此文件

DirectoryIndex install.php index.php

RewriteEngine on
RewriteBase /myscript/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewrietRule . index.php [E]


如果用户转到安装应用程序的目录,例如
http://example.com/myscript/
,它将首先尝试加载install.php,然后如果不存在,它将尝试加载index.php。如果用户转到
http://example.com/myscript/index.php
,您运气不好,但转到该url毫无意义!如果您担心这个问题,最好使用romaneso的答案,始终加载index.php,必要时从那里加载install.php。

谢谢。但是,我知道。可以从.htaccess进行吗?我不会在.htaccess中进行,我会用php编程。如果install.php文件存在,我只会在index.php-文件中进行检查,如果是,则会重新通知用户。传递install.php文件后,该文件将被删除。谢谢。但是,我知道。可以从.htaccess进行吗?我不会在.htaccess中进行,我会用php编程。如果install.php文件存在,我只会在index.php-文件中进行检查,如果是,则会重新通知用户。传递install.php-文件后,该文件将被删除。您可以改为加载index.php(因为这是您将使用99%的时间)并检查该文件中是否存在install.php。如果是,则
require(…)那个。为什么要在.htaccess中这样做?只是为了好玩,还是你有什么特殊的原因?如果你不让安装你的应用程序的人进行apache配置,即使是通过htaccess,他们也会很感激。有些将不启用modrewrite,有些甚至可能无法使用htaccess.Ok@ScottSaunders。注:)您可以改为加载index.php(因为99%的时间都在使用index.php),并在该文件中检查install.php是否存在。如果是,则
require(…)那个。为什么要在.htaccess中这样做?只是为了好玩,还是你有什么特殊的原因?如果你不让安装你的应用程序的人进行apache配置,即使是通过htaccess,他们也会很感激。有些将不启用modrewrite,有些甚至可能无法使用htaccess.Ok@ScottSaunders。注:)哈,几乎同时相同。:)非常感谢。你们能告诉我
^
之间的区别吗?@Jigar
^
表示行的开头<代码>
表示“匹配一个字符”。因此,如果您只访问
site.com/myscript
^
将始终匹配,而
可能不匹配。实际上,请使用
^
我刚刚粘贴了您的第一条重写规则,因为我最关注的是问题所在的第二条重写规则。为此,我将投票给@zx81Thanks@zx81,我必须正确选择她的答案,因为她是第一个,她写道,因为我的问题。:)哈,几乎同时相同。:)非常感谢。你们能告诉我
^
之间的区别吗?@Jigar
^
表示行的开头<代码>
表示“匹配一个字符”。因此,如果您只访问
site.com/myscript
^
将始终匹配,而
可能不匹配。实际上,请使用
^
我刚刚粘贴了您的第一条重写规则,因为我最关注的是问题所在的第二条重写规则。为此,我将投票给@zx81Thanks@zx81,我必须正确选择她的答案,因为她是fir