【问题标题】:Access install.php if install.php is present, else index.php如果存在 install.php,则访问 install.php,否则访问 index.php
【发布时间】:2014-09-13 05:59:05
【问题描述】:

在访问我的 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 是否存在的 RewriteCond:

RewriteEngine On
RewriteBase /myscript/
RewriteCond ?? ??
RewriteRule ^ install.php [E]

感谢任何帮助。顺便说一句,我对 .htaccess 配置一点也不擅长。

更新:

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

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

但是我们可以对 .htaccess 做同样的事情吗?

【问题讨论】:

  • 您可以改为加载 index.php(因为这将是您 99% 的时间使用的内容)并在该文件中检查 install.php 是否存在。如果是,那么require( ... ); 那。
  • 为什么要在 .htaccess 中这样做?只是为了好玩还是有什么特殊原因?
  • 如果您不让他们进行 apache 配置,即使是通过 htaccess,许多安装您的应用程序的人都会喜欢它。有些不会启用 modrewrite,有些甚至可能无法使用 htaccess。
  • 好的@ScottSaunders。著名的。 :)

标签: php regex apache .htaccess mod-rewrite


【解决方案1】:

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

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

说明

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

【讨论】:

  • 仅供参考:添加说明。 :)
  • I had to select her answer right as she was first, she wrote . coz of my question 不,伙计,这对我来说毫无意义。你应该选择一个有效的答案。
  • 提供最完整的答案是您应得的
  • 感谢@ValentinMercier,您是一位绅士,我们真的在 RewriteCond 上融为一体。 :)
【解决方案2】:

对于纯.htaccess 解决方案:

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

【讨论】:

  • 哈,同时几乎一模一样。 :)
  • 谢谢。你们能告诉我.^ 之间的区别吗?
  • @Jigar ^ 表示行首。 . 表示“匹配一个字符”。所以如果你只是去site.com/myscript^会一直匹配,而.可能不匹配。
  • 确实宁愿使用^ 我刚刚粘贴了您的第一个重写规则,因为我最关注的是问题所在的 RewrtieCond。为此我会投票@zx81
  • 谢谢@zx81,我必须先选择她的答案,因为她写了.我的问题。 :)
【解决方案3】:

您可以使用DirectoryIndex 来实现您想要的(docs)。在应用程序根目录的.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。

【讨论】:

    【解决方案4】:

    如果文件存在,我会尝试使用该方法检查

    bool file_exists ( string $filename )
    

    【讨论】:

    • 谢谢。但是,我知道。是否可以从 .htaccess 中获得?
    • 我不会在 .htaccess 中这样做,我会在 php 中进行编程。
    • 如果 install.php 文件存在,我将只检查 index.php- 文件,如果存在,则用户被重定向到它。通过 install.php- 文件后,该文件被删除。
    猜你喜欢
    • 2020-09-01
    • 2012-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-14
    • 2012-03-06
    • 2013-12-04
    相关资源
    最近更新 更多