存在RewriteCond文件

编程入门 行业动态 更新时间:2024-10-12 18:15:23
本文介绍了存在RewriteCond文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是一个简单的问题,但是htaccess和我从来没有相处过。

Such a simple problem but htaccess and I have never got along.

  • 提供文件(如果存在)。
  • 如果URI只是/并且存在index.html,则提供index.html
  • 否则,提供app.php
  • 这里是.htaccess:

    Here is .htaccess:

    # Disable the directory processing, Apache 2.4 DirectoryIndex disabled xxx RewriteEngine On # Serve existing files RewriteCond %{REQUEST_FILENAME} -f RewriteRule .? - [L] # For / serve index.html if it exists RewriteCond %{REQUEST_URI} ^/$ RewriteCond index.html -f RewriteRule .? index.html [L] # Otherwise use front controller RewriteRule .? app.php [L]

    如果我将RewriteCond index.html -f行注释为只要index.html确实存在。但是我也想检查index.php,所以我需要检查文件是否存在。而且如果没有别的我想了解为什么张贴的行不起作用。

    It works if I comment out the RewriteCond index.html -f line as long as index.html does in fact exists. But I also want to check for index.php so I need the file exists check. And if nothing else I'd like to understand why the posted lines do not work.

    推荐答案

    这应该起作用:

    # Disable the directory processing, Apache 2.4 DirectoryIndex disabled xxx RewriteEngine On # Serve existing files RewriteCond %{REQUEST_FILENAME} -f RewriteRule ^ - [L] # For / serve index.html if it exists RewriteCond %{DOCUMENT_ROOT}/index.html -f [NC] RewriteRule ^/?$ index.html [L] # Otherwise use front controller RewriteRule ^ app.php [L]

    请记住 RewriteCond -f 或 -d 的文件或目录需要完整路径,这就是为什么需要在文件名前加上%{DOCUMENT_ROOT} / 。无需在这里转义。

    Remember that RewriteCond with -f or -d needs full path to the file or directory that's why you need to prefix the filename with %{DOCUMENT_ROOT}/. No need to escape dots here.

    更多推荐

    存在RewriteCond文件

    本文发布于:2023-11-02 03:46:36,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1551281.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:文件   RewriteCond

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!