【问题标题】:How to exclude a specific file in htaccess如何在 htaccess 中排除特定文件
【发布时间】:2011-07-08 07:21:28
【问题描述】:

我在 htaccess 中写了下面的代码

重写规则 ^([a-zA-Z_-]+).php$ http://www.domain.com/myfile.php?page=inc-$1.php [NC]

我不想重写 index.php 文件 - 它应该正常打开。

【问题讨论】:

    标签: .htaccess


    【解决方案1】:

    您可以使用以下代码来排除所有现有文件(如 index.php):

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([a-zA-Z_-]+).php$ http://www.domain.com/myfile.php?page=inc-$1.php [NC]
    

    或仅排除 index.php 您可以使用:

    RewriteCond %{REQUEST_URI} !^(.*/)?index\.php$ [NC]
    RewriteRule ^([a-zA-Z_-]+).php$ http://www.domain.com/myfile.php?page=inc-$1.php [NC]
    

    【讨论】:

    • 您的意思是 RewriteCond %{REQUEST_FILENAME} !-index.php 将允许以常规方式打开 index.php。正确的 ?谢谢
    • 不,!-index.php 无效。 -f 表示文件存在,!-f 表示文件不存在。
    • 我添加了另一种您可以使用的方法。
    【解决方案2】:

    认为这就是你要问的:

    "我想允许 index.php 文件 正常送达,但所有其他 请求遵循此重写..."

    为了防止索引文件被重定向,你需要在规则前加上一个 RewriteCond:

    RewriteCond $1 !index\.php
    RewriteRule ^([a-zA-Z_-]+).php$ http://www.domain.com/myfile.php?page=inc-$1.php [NC]
    

    【讨论】:

    • 以上编辑允许更大范围的匹配 - 去掉了 ^ 和 $
    猜你喜欢
    • 1970-01-01
    • 2015-12-11
    • 2018-10-16
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 2018-01-23
    • 2018-06-27
    • 1970-01-01
    相关资源
    最近更新 更多