首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RewriteCond文件存在

RewriteCond文件存在
EN

Stack Overflow用户
提问于 2015-03-28 18:05:05
回答 1查看 8.9K关注 0票数 3

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

  1. 如果文件存在,则提供该文件。
  2. 如果URI只是/如果存在index.html,那么服务于index.html
  3. 否则服务于app.php

以下是.htaccess:

代码语言:javascript
复制
# 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,所以我需要文件存在检查。如果没有其他的话,我想了解为什么张贴的线路不能工作。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-28 18:35:40

这应该是可行的:

代码语言:javascript
复制
# 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]

请记住,带有-f-d-d需要文件或目录的完整路径,这就是为什么需要在文件名前加上%{DOCUMENT_ROOT}/的原因。没必要在这里避开点。

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29320684

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档