如果文件或目录不存在,则重定向


redirecting if file or directory not exists

我有一个简单的CMS文件结构:

application/
data/
src/
web/
.htaccess
index.php

如果文件或目录存在,我想将所有请求重定向到/index.php execept,然后我想将全部重定向到/web。示例:http://localhost.com/news/example-news到index.php/http://localhost.com/images/logo.png到web/images/logo.png

现在我的.htaccess看起来:.htaccess

试试这个。。。

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ /web [L]
RewriteRule ^.*$ index.php [L]

第一条规则将捕获任何可以映射到现有文件或目录并重写到web文件夹的请求。第二条规则(不带rewritecond)将捕获剩余的并重写index.php

您只需要将代码更改为这样。

更改此

RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [NC,L]
RewriteRule ^(/)?$ index.php [L]

到这个

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^(.*)$ /web/index.php [L]
RewriteRule ^(.*)$ index.php [NC,L]