如果子目录中存在,则重写到文件
Rewrite to file if that exists in subdirectory
我有这样的文件结构:
root
|- public
|- file.jpeg
|- file.txt
|- .htaccess
|- index.php
现在,我想将 example.com/file.jpeg
重写为 example.com/public/file.jpeg
,file.txt 和 public
目录中的所有其他文件也是如此,但是如果文件没有t 存在于子目录中然后重写为 index.php
出于某种原因,此 .htaccess 将所有内容重写为 index.php
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/public/ -f
RewriteRule ^(.*)$ public/ [L]
RewriteRule ^(.*)$ index.php [L]
我的代码有什么问题?
您缺少 RewriteCond
检查上一条规则中的现有 files/directories,因此 将所有内容 路由到 index.php
。
您可以使用:
RewriteEngine On
RewriteBase /
# if direct access to /public needs be directed to index.php
RewriteCond %{THE_REQUEST} \s/+public [NC]
RewriteRule ^ index.php [L]
RewriteCond %{DOCUMENT_ROOT}/public/ -f
RewriteRule ^(.*)$ public/ [L]
# if it is not /public/ then route to index.php
RewriteRule !^public/ index.php [L,NC]
我有这样的文件结构:
root
|- public
|- file.jpeg
|- file.txt
|- .htaccess
|- index.php
现在,我想将 example.com/file.jpeg
重写为 example.com/public/file.jpeg
,file.txt 和 public
目录中的所有其他文件也是如此,但是如果文件没有t 存在于子目录中然后重写为 index.php
出于某种原因,此 .htaccess 将所有内容重写为 index.php
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/public/ -f
RewriteRule ^(.*)$ public/ [L]
RewriteRule ^(.*)$ index.php [L]
我的代码有什么问题?
您缺少 RewriteCond
检查上一条规则中的现有 files/directories,因此 将所有内容 路由到 index.php
。
您可以使用:
RewriteEngine On
RewriteBase /
# if direct access to /public needs be directed to index.php
RewriteCond %{THE_REQUEST} \s/+public [NC]
RewriteRule ^ index.php [L]
RewriteCond %{DOCUMENT_ROOT}/public/ -f
RewriteRule ^(.*)$ public/ [L]
# if it is not /public/ then route to index.php
RewriteRule !^public/ index.php [L,NC]