【问题标题】:Apache, .htaccess, multiple index pageApache,.htaccess,多索引页面
【发布时间】:2016-12-12 11:08:01
【问题描述】:

我想要一个在每个请求中执行的文件,比如说index.php

RewriteRule ^(.*)$ index.php [QSA]

然而,如果它碰巧有index2.php,我们宁愿执行那个文件。换句话说,如果index2.php 存在,它会覆盖默认行为。所以我会这样写:

RewriteRule ^(.*)$ index2.php [QSA]
RewriteRule ^(.*)$ index.php [QSA]

但如果index2.php 不存在,则会失败

RewriteRule ^(.*)$ index2.php [L]
RewriteRule ^(.*)$ index.php [L]

也是这样。 Index2.php 不需要存在,但index.php 确实存在并且将永远存在

【问题讨论】:

    标签: php apache .htaccess


    【解决方案1】:

    您可以添加条件,仅当 index2.php 存在时才应用RewriteRule

    RewriteCond %{DOCUMENT_ROOT}/index2.php -f
    RewriteRule ^(.*)$ index2.php [QSA,L]
    

    【讨论】:

    • 您还应该添加L 标志。 %{DOCUMENT_ROOT}/index2.php 是正确的路径吗?
    • 通常与您通常要添加的那些规则 RewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_FILENAME} !-f (两者)
    • 您可以使用 RewriteRule 标志 S|skip 将多个 RewriteRules 绑定到单个 RewriteCond。否则,您需要为两个 RewriteRules 重复相同的 RewriteConds。
    • 我明天早上做,我现在在手机上;请记住这一点:stackoverflow.com/questions/25520448/…
    • 你的意思是“为根”?有索引页吗?
    猜你喜欢
    • 1970-01-01
    • 2012-05-24
    • 2015-03-05
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 2012-06-14
    • 1970-01-01
    • 2022-01-22
    相关资源
    最近更新 更多