9

我在 index.php 中有一个调度程序函数,因此 URL 如下:

/博客/节目转到

/index.php/blog/show

<IfModule mod_rewrite.c>
    重写引擎开启
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

如何修改它,以便我可以将所有静态文件转储到公共目录中,但在 URL 中不公开的情况下访问它们。

例如 /docs/lolcats.pdf 访问

/public/docs/lolcats.pdf 在驱动器上

我试过这个

RewriteCond %{REQUEST_FILENAME} !f
RewriteRule ^(.*)$ public/$1 [QSA,L]
4

1 回答 1

15

试试这个:

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

如果要在根目录下的子目录中使用它:

RewriteCond %{REQUEST_URI} ^/subdir(/.*)
RewriteCond %{DOCUMENT_ROOT}subdir/public%1 -f
RewriteRule !^public/ public%1 [L]

我找到了另一个没有明确命名子目录的解决方案:

RewriteRule ^public/ - [L]
RewriteCond public/$0 -F
RewriteRule .* public/$0 [L]

重要的变化是使用-F而不是-f前者触发子请求。

于 2009-01-28T11:48:24.747 回答