Apache将所有重定向到index.php,现有文件和文件夹除外 [英] Apache redirect all to index.php except for existing files and folders
问题描述
我有index.php通过$_SERVER[‘REQUEST_URI’]
变量读取完整路径.我的任务是当用户输入以下内容时:www.domain/resource/777
使用路径/resource/777
重定向到index.php
并解析$_SERVER[‘REQUEST_URI’]
以执行一些逻辑.但是我也有真实的文件和文件夹,例如:
I have index.php that reads full path by $_SERVER[‘REQUEST_URI’]
variable. My task is when user enter: www.domain/resource/777
redirect to index.php
with path /resource/777
and parse $_SERVER[‘REQUEST_URI’]
to do some logic. But I have also real files and folders like:
- css/theme.css
- assets/assets.js
- resource/locale.js
当我尝试此配置时:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php [L]
客户端无法获取所有的css等文件.如何在Apache中编写正确的规则以实现这一目标?
Client could not get all css and etc files. How to write correct rule in Apache to achive this?
推荐答案
如果您使用的是Apache 2.2.16或更高版本,则可以用一个指令完全替换重写规则:
If you are using Apache 2.2.16 or later, you can replace rewrite rules entirely with one single directive:
FallbackResource /index.php
请参见 https://httpd.apache.org/docs/2.2/mod/mod_dir.html#fallbackresource
基本上,如果请求将导致错误404,则将使用后备资源uri代替该请求,以正确填充$_SERVER[‘REQUEST_URI’]
变量.
Basically, if a request were to cause an error 404, the fallback resource uri will be used to handle the request instead, correctly populating the $_SERVER[‘REQUEST_URI’]
variable.
这篇关于Apache将所有重定向到index.php,现有文件和文件夹除外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!