apache - .htaccess:提供静态文件,将其他所有内容路由到 index.php

标签 apache .htaccess webfaction

我想使用 .htaccess file 检查请求的路径是否是 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 .

最佳答案

此代码应该在您这边按预期工作

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]

关于apache - .htaccess:提供静态文件,将其他所有内容路由到 index.php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25956054/

相关文章:

php - 服务器 ip 不适用于站点测试

Apache2 登录 access.log1 而不是 access.log

.htaccess - 在 .htaccess 中强制使用 WWW 和 HTTPS (SSL) -- 需要帮助

apache - 配置 htaccess 进行 url 重写

Django Postgres 未从环境变量中读取数据库密码

apache - 将 www.domain.com/test.php 重写为 www.domain.com/test/

.htaccess - 删除 URL 重写不起作用的子文件夹

python - 网络主机 webfaction : site navigation

mongodb - 在 Webfaction 上安装 Meteor

php - 您无权在 CentOS7 VPS 服务器中访问此服务器上的/phpMyAdmin