【问题标题】:.htaccess: Serve static files, route everything else to index.php.htaccess:提供静态文件,将其他所有内容路由到 index.php
【发布时间】:2014-11-15 08:31:23
【问题描述】:

我想使用.htaccess 文件来检查请求的路径是否是public/ 目录中的文件。如果是,请提供服务,否则将请求转发至/index.php。我似乎无法让它工作。

这是我得到的:

Options +FollowSymLinks
RewriteEngine on

Options -Indexes

RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -f
RewriteRule ^ %{DOCUMENT_ROOT}/public%{REQUEST_URI} [L]

RewriteRule ^ index.php [QSA,L]

例如http://example.com/css/style.css 应该有 apache 服务 /public/css/style.css 因为它是一个存在的文件,但 http://example.com/css/style.bad 应该发送到 /index.php

【问题讨论】:

    标签: apache .htaccess webfaction


    【解决方案1】:

    显然是[L] does not work as I expected。考虑到这一点,经过大量的试验和错误,我设法找到了可行的方法:

    RewriteEngine On
    
    RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -f
    RewriteRule ^ public%{REQUEST_URI} [L]
    
    RewriteCond %{REQUEST_URI} !^/public/
    RewriteRule ^ index.php [QSA,L]
    

    【讨论】:

      【解决方案2】:

      此代码在您这边应该可以正常工作

      Options +FollowSymLinks -MultiViews -Indexes
      
      RewriteEngine On
      RewriteBase /
      
      RewriteCond %{DOCUMENT_ROOT}/public/$1 -f
      RewriteRule ^(.*)$ public/$1 [L]
      
      RewriteCond %{THE_REQUEST} \s/public/ [NC,OR]
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule ^ index.php [L,QSA]
      

      【讨论】:

      • 行得通!谢谢你。我只是注意到style.css 可以在 both /css/style.css /public/css/style.css 使用。从技术上讲,后者也应该路由到index.php,因为/public/public/css.style.css 不是文件。这也意味着/public/xxx 将抛出一个apache 404,而不是我在index.php 文件中配置的自定义一个。没有办法解决这个问题,是吗?
      猜你喜欢
      • 2017-04-07
      • 1970-01-01
      • 1970-01-01
      • 2017-07-27
      • 2019-08-23
      • 1970-01-01
      • 2021-12-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多