【问题标题】:How can I check whether file exists or doesn't in specific folder in .htaccess?如何检查.htaccess 中特定文件夹中是否存在文件?
【发布时间】:2013-03-16 17:26:30
【问题描述】:

我使用这个代码:

RewriteCond public/%{REQUEST_FILENAME} -f [OR]
RewriteCond public/%{REQUEST_FILENAME} -d 
RewriteRule public/.* - [L]

它不起作用。

例如,我想要那个 URL: http://example.com/style.css 被这样插话

public/style.css

如果 style.css 存在于 public 目录中。谢谢你:)

【问题讨论】:

    标签: .htaccess url mod-rewrite uri


    【解决方案1】:

    如果我没记错的话:

    RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -f [OR]
    RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -d 
    RewriteRule public/.* - [L]
    

    编辑

    %{REQUEST_FILENAME} 包含所请求文件的完整路径。所以例如/var/www/example.com/index.html。所以在前面加上public/会得到public/var/www/example.com/index.html,它永远不存在。因此,您使用包含/var/www/example.com%{DOCUMENT_ROOT} 和包含/index.html%{REQUEST_URI}

    另请注意,-f-d 需要完整路径,而不仅仅是相对于 wwwroot 的路径。

    【讨论】:

    • 没问题。请参阅上面的编辑。如果您还有其他问题,请告诉我。
    • 怎样才能让所有不存在的文件都返回404?
    • 我不明白。无论上述重写规则如何,所有不存在的文件都应该只导致 404。
    猜你喜欢
    • 2018-11-17
    • 1970-01-01
    • 1970-01-01
    • 2019-03-26
    • 2021-07-09
    • 1970-01-01
    • 2011-11-15
    相关资源
    最近更新 更多