regex - RewriteCond 检查目录是否存在索引文件

标签 regex apache .htaccess mod-rewrite

我正在尝试对所有请求进行重写,除非 URL 指向:

  • 现有文件
  • 带有索引文件的目录

  • 所以我有以下应该处理它:
    RewriteCond %{REQUEST_FILENAME} !-s
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule ^(.*)$ redirect.php [QSA]
    

    但是当我导航到包含 index.php 的文件夹时(是的,我已经检查过 DirectoryIndex 设置为 index.php ),它仍然执行重写。

    我还发现有趣的是,如果我有一个 .htaccess在该文件夹中使用 RewriteEngine on (仅此而已),那么上面的重写规则有效......

    任何指针?

    最佳答案

    在您的根 .htaccess 中尝试此规则:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{DOCUMENT_ROOT}/$1/index.php !-f
    RewriteRule ^(.*?)/?$ redirect.php [L]
    

    关于regex - RewriteCond 检查目录是否存在索引文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19663940/

    相关文章:

    php - 如何从网络创建不可访问的 xampp 服务器?

    linux - 如何使用 'pycurl' 和 'threading' 模块在 python 2.6 中多线程 URL 请求?

    php - .htaccess 301 使用 php get 查询重定向(旧 url 到新更新)

    .htaccess - 如何合并两个 .htaccess 重定向?

    .htaccess - htaccess 子域重写

    带可选参数的 python re.search

    java - 我应该如何在Java中使用正则表达式和分割方法获得结果?

    c++ - 为什么 regex_search 不匹配任何东西?

    wordpress - 如何用 %23 重写 URL?

    java - 替换所有捕获的组