【问题标题】:.htaccess to allow only pdf files in a subdirectory.htaccess 仅允许子目录中的 pdf 文件
【发布时间】:2016-12-24 13:31:52
【问题描述】:

我正在尝试编写一个.htaccess 文件以仅允许访问子目录中的 pdf 文件。我将在我无法控制的主机上部署文件,因此我无法更改 apache 配置。

我只想允许访问 Foo 目录中的 .pdf 文件。我尝试过:

Deny From All

<FilesMatch ".+\/Foo\/.+\.pdf$">
Allow From All
</FilesMatch>

但是,当我尝试访问 example.com/bar/Foo/baz.pdf 时,我收到了 HTTP 403 Forbidden 响应。

我怎样才能拒绝访问除特定目录中的 pdf 文件之外的所有内容?

谢谢

【问题讨论】:

标签: apache .htaccess


【解决方案1】:

在根 .htaccess 中创建一个 this 作为您的第一条规则:

RewriteEngine On

# prohibit everything except .pdf files    
RewriteRule ^foo/(?!.*\.pdf$) - [F,NC]

【讨论】:

  • 这行得通,但我不想把它放在子目录中。我怎样才能扩展正则表达式?我试过!^.+\/Foo\/.+\.pdf$,但对我没有用。
  • 我想我现在明白了。我想要得到的是RewriteRule !^Foo\/.+\.pdf$ - [F,NC]。谢谢!
  • 完成。感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-03
  • 1970-01-01
  • 2018-04-19
  • 1970-01-01
  • 2019-10-22
  • 1970-01-01
相关资源
最近更新 更多