大网数据广告活动
服务器运维问答

How to match beginning of file name using htaccess

· 52CE 我爱测

I am attempting to modifying my .htaccess file within a specific directory. If a web user attempts to find any file in this directory that may be named like the following options, I want them to be redirected back to home. Below …

问题原文

原帖链接 · klewis · CC BY-SA 4.0

I am attempting to modifying my .htaccess file within a specific directory. If a web user attempts to find any file in this directory that may be named like the following options, I want them to be redirected back to home. Below are some file name examples.

  • /cat_1234.pdf
  • /cat_blahbla.doc
  • /cat_$9989&428.jpg
  • /cat_-309bn-020n.webp

...how can I tell my RewriteCond to look out for these patterns? Here was my best attempt, which I thought would work, but it doesn't...

<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_URI} ^cat_([0-9a-zA-Z_]+)\.(pdf|doc|jpg|webp) [NC]
RewriteRule . /index.php [R=302,L]
</IfModule>

What am I missing?

已采纳解答

原帖链接 · MrWhite · CC BY-SA 4.0

You've not stated the "specific directory" in which the .htaccess file and files you are protecting is located? (Although that shouldn't matter if we rework the rule.)

RewriteCond %{REQUEST_URI} ^cat_([0-9a-zA-Z_]+)\.(pdf|doc|jpg|webp) [NC]
RewriteRule . /index.php [R=302,L]

The REQUEST_URI server variable contains the full URL-path (including the slash prefix), so this would normally need to include the "specific directory", not just the filename (unless you adjust the regex). You have a start-of-string anchor on the regex (although you have omitted the end-of-string anchor) so this condition (RewriteCond directive) will never match.

Your regex would also fail to match your 3rd and 4th examples because your regex character class ([0-9a-zA-Z_]) omits the special characters $, & and - that are present in these filenames. Although I would surmise you do not need to be so specific and catching cat_<anything>.pdf (for example) would be OK.

However, you do not need a separate condition here. It is easier and more efficient to just use the RewriteRule pattern, which matches relative to the directory that contains the .htaccess file (and excludes the slash prefix), so you do not need to worry about the rest of the URL-path.

I also doubt that you should be redirecting to /index.php. Should this not be simply / (the root directory) and allow the directory index (ie. index.php) to be served by mod_dir? Is that not your canonical URL?

Try the following instead, in the .htaccess file in the directory you are protecting.

RewriteRule ^cat_[^/]+\.(pdf|doc|jpg|webp)$ / [R=302,L]

This regex is perhaps slightly more broad than it needs to be, but that also makes it simpler. ie. [^/] matches anything that is not a / (path separator).

And no need for the <IfModule> wrapper, unless this rule is entirely optional.


However, instead of redirecting to the homepage (which is confusing for users and unnecessary for bots) I would simply block (with a 403 Forbidden) such requests instead. For example:

<FilesMatch "^cat_[^/]+\.(pdf|doc|jpg|webp)$">
    Require all denied
</FilesMatch>

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2024-03-28)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。