【问题标题】:public directory for .htaccess.htaccess 的公共目录
【发布时间】:2010-10-01 04:28:26
【问题描述】:

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

/blog/show 转到

/index.php/blog/show


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

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

例如 /docs/lolcats.pdf 访问

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

我试过了

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

【问题讨论】:

    标签: php regex .htaccess mod-rewrite url-rewriting


    【解决方案1】:

    试试这个:

    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,因为前者会触发子请求。

    【讨论】:

    • 谢谢你,这很完美!我正在尝试修改它以使其嵌套在另一个目录中,但未成功。例如,如果文件 foo.txt 放在 DOCROOT/mysubdir/public/foo.txt 中,什么规则允许我通过 www.example.com/mysubdir/foo.txt 访问它?
    • 我认为只有明确命名子目录才有可能。
    • 这个答案的第二部分完全符合我的需要 - 在“公共”目录中显示文件,而无需在 url 中指定“公共”。
    • @Gumbo 我需要与此类似的答案。请阅读我的给我任何解决方案 - stackoverflow.com/questions/57992993/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多