简体   繁体   English

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

[英].htaccess: Serve static files, route everything else to index.php

I want to use an .htaccess file to check if the requested path is a file in the public/ directory. 我想使用.htaccess文件来检查请求的路径是否是public/目录中的文件。 If yes, serve it, else forward request to /index.php . 如果是,请提供服务,否则将请求转发给/index.php I can't seem to get this to work. 我似乎无法让这个工作。

Here's what I've got: 这是我得到的:

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]

eg http://example.com/css/style.css should have apache serve /public/css/style.css because it's a file that exists, but http://example.com/css/style.bad should be sent to /index.php . 例如http://example.com/css/style.css应该有apache serve /public/css/style.css因为它是一个存在的文件,但应该发送http://example.com/css/style.bad/index.php

This code should be working as expected on your side 此代码应该按预期工作

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]

Apparently [L] does not work as I expected . 显然[L]不能像我预期的那样工作 With that in mind, and a lot of trial and error, I managed to find something that works: 考虑到这一点,以及大量的反复试验,我设法找到了有效的方法:

RewriteEngine On

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

RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^ index.php [QSA,L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM