.htaccess的公共目录 [英] public directory for .htaccess
问题描述
我在index.php中具有调度程序功能,因此URL类似于:
I have a dispatcher function in index.php so URLs like:
/博客/显示转到
/index.php/blog/show
/index.php/blog/show
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
如何修改此设置,以便可以将所有静态文件转储到公共目录中,但在URL中不公开的情况下访问它们.
How can I modify this so that I can dump all of my static files into a public directory but access them without public in the URL.
例如/docs/lolcats.pdf访问权限
For example /docs/lolcats.pdf accesses
/public/docs/lolcats.pdf在驱动器上
/public/docs/lolcats.pdf on the drive
我尝试过
RewriteCond %{REQUEST_FILENAME} !f
RewriteRule ^(.*)$ public/$1 [QSA,L]
推荐答案
尝试一下:
RewriteCond %{DOCUMENT_ROOT}public%{REQUEST_URI} -f
RewriteRule !^public/ public%{REQUEST_URI} [L]
如果要在根目录下的子目录中使用它:
If you want to use it in a subdirectory below the root:
RewriteCond %{REQUEST_URI} ^/subdir(/.*)
RewriteCond %{DOCUMENT_ROOT}subdir/public%1 -f
RewriteRule !^public/ public%1 [L]
我找到了另一种没有明确命名子目录的解决方案:
I found another solution without explicitly naming the subdirectory:
RewriteRule ^public/ - [L]
RewriteCond public/$0 -F
RewriteRule .* public/$0 [L]
重要的更改是使用-F
而不是-f
,因为前者会触发子请求.
The important change was using -F
instead of -f
as the former triggers a subrequest.
这篇关于.htaccess的公共目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!