【问题标题】:htaccess: How to allow access only to one file, otherwise request index.php?htaccess:如何只允许访问一个文件,否则请求 index.php?
【发布时间】:2026-02-01 19:35:01
【问题描述】:

如何只允许访问一个文件,否则使用.htaccess 请求index.php

我只需要允许访问文件 update.php。如果不是这个文件,那么只需访问 index.php。 我试过这段代码:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} ^update\.php$ [NC]
    RewriteRule . index.php [L]
</IfModule>

但它允许访问所有文件。我不知道为什么。 希望得到您的帮助!

【问题讨论】:

    标签: .htaccess web mod-rewrite


    【解决方案1】:

    就这样吧:

    RewriteEngine On
    
    RewriteCond %{REQUEST_URI} !^/update\.php$ [NC]
    RewriteRule ^ index.php [L]
    

    【讨论】:

      【解决方案2】:

      根据您显示的示例,您能否尝试以下操作。请确保在测试您的 URL 之前清除您的浏览器缓存。

      RewriteEngine ON
      RewriteCond %{REQUEST_FILENAME} -f
      RewriteRule ^(!?update\.php).*$ - [NC,F,L]
      
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^ index.php [L]
      

      【讨论】: