.htaccess 重定向和防止热链接的正确正则表达式 [英] Proper regex for .htaccess redirect and prevention of hotlinks

查看:19
本文介绍了.htaccess 重定向和防止热链接的正确正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Reddit (r/learnprogramming) 上发布了这篇文章,那里有人 PM 我并告诉我来这里,所以我来了!

我一直在努力学习正则表达式,但我仍然很讨厌它们.我真的很难掌握模式匹配.我精通其他 OOP 语言,所以我想我会学习正则表达式,但它只是逃避了我.

我已经下载了 EditPad Pro,所以我可以练习 http://www.regular-expressions.info/tutorial.html 建议.我可以获得匹配批量文本的表达式,但我正在尝试解析 URL 并且我一直在丢失.

这就是我想要做的.我正在编写我自己的永久链接 .htaccess 文件作为概念研究的证明,所以我希望可以在未来的网站中使用它.

我需要从 URL 返回以下动态内容:

我需要除 http://www.domain.com/或 http://domain.com/或 domain.com/之外的所有内容:
(由于新账户的限制,我在 http://后面加了一个空格)

http://www.domain.com/asdjh324hj.jpg
http://www.domain.com/asa45s.png
http://www.domain.com/aser24hj.gif/
http://www.domain.com/wer234dsfa/
http://www.domain.com/k3kjk4
http://www.domain.com/k3kasd4/

匹配的部分将被附加到:

http://www.domain.com/some_dir/som_subdir/some_file.php?querystring=$1

但是,我不希望结果中包含任何这些网址:

http://www.domain.com/some_dir/some_file.php
http://www.domain.com/some_dir/some_subdir/some_file.html

而且我需要防止盗链到 image_dir 中的图像:

http://www.domain.com/image_dir/some_dir/some_subdir/some_image.jpg(或 png、gif 等)

热链接的图像将被重定向到一个页面,并将传递的图像作为查询字符串.

那么我会设置什么 RewriteRule 正则表达式来获取它?我了解 RewriteRules 和标志,将匹配的结果放入变量等,我只是不知道我应该写什么正则表达式来获取实际结果.

如果这对 RewriteRules 来说太复杂了,那么请告诉我,因为我在这里挣扎.

通常我用 PHP 来做这些,并且会从:
.com/[a-zA-Z0-9-_.]+
([^/]+)/?$
然后做好'ol 子串和检查.它正在把它砍死,我应该做得更好!

我目前正在阅读正则表达式.info教程并且正在取得进展,但我也一直在抓住错误的东西.

感谢您的帮助!

更新:我能够通过大量帮助解决所有问题,并在此处进行了更多讨论:Mod_Rewrite 条件有助于盗链但允许本地请求

解决方案

我需要除 http://www.domain.com/或 http://domain.com/或 domain.com/之外的所有内容:

RewriteCond %{REQUEST_URI} !^/$

<块引用>

但是,我不希望结果中包含任何这些网址:

RewriteCond %{REQUEST_URI} !^/some_dir/

<块引用>

匹配的部分将被附加到:

RewriteRule ^(.*)$/some_dir/som_subdir/some_file.php?querystring=$1 [L]

所以它应该看起来像这样:

RewriteCond %{REQUEST_URI} !^/$RewriteCond %{REQUEST_URI} !^/some_dir/重写规则 ^(.*)$/some_dir/som_subdir/some_file.php?querystring=$1 [L]

当您请求诸如 http://www.domain.com/asa45s.html 之类的内容时,它将在内部重写为 some_dir/som_subdir/some_file.php?querystring=asa45s.html.至于盗链位:

RewriteCond %{REQUEST_URI} ^/image_dir/RewriteCond %{REQUEST_URI} \.(png|gif|jpe?g|bmp|ico)$ [NC]RewriteCond %{HTTP_REFERER} !^https?://(www\.)?domain.com/重写规则 ^(.*)$/some_dir/som_subdir/some_file.php?querystring=$1 [L]

这首先检查请求是针对 /image_dir/ 目录中的内容,然后是请求的资源以 png/gif/jpeg/bmp/ico 扩展名结尾,然后是 HTTP 引用[原文如此] 不以 http://www.domain.com/https://domain.com/ 或这 2 的任何组合开头.如果所有这些为真,然后它将请求重写到 /some_dir/som_subdir/some_file.php 文件,并将原始 URI 作为 querystring 参数.

I posted this on Reddit (r/learnprogramming) and someone there PM'd me and told me to come here, so here I am!

I have been trying to learn regex's and I suck at them still. I seriously have difficulty grasping the pattern matching. I am solid in other OOP languages so I figured I would learn regex and it just evades me.

I have downloaded EditPad Pro so I can practice as http://www.regular-expressions.info/tutorial.html suggests. I can get expressions to match bulk text, but I am trying to parse URL's and I keep missing.

Here is what I am trying to do. I am writing my own permalink .htaccess file as a proof of concept study, so I can hopefully use this is in future sites.

I need to return the following dynamic content from a URL:

I need everything other than http:// www.domain.com/ or http:// domain.com/ or domain.com/:
(I am adding a space after http:// because of the limits on new accounts)

http:// www.domain.com/asdjh324hj.jpg
http:// www.domain.com/asa45s.png
http:// www.domain.com/aser24hj.gif/
http:// www.domain.com/wer234dsfa/
http:// www.domain.com/k3kjk4
http:// www.domain.com/k3kasd4/

The matched part will then be appended to:

http:// www.domain.com/some_dir/som_subdir/some_file.php?querystring=$1

But, I don't want any of these urls in the results:

http:// www.domain.com/some_dir/some_file.php
http:// www.domain.com/some_dir/some_subdir/some_file.html

And I need to prevent hotlinking to images in the image_dir:

http:// www.domain.com/image_dir/some_dir/some_subdir/some_image.jpg (or png,gif,etc)

Hotlinked images would be redirected to a page with the passed image as a querystring.

So what RewriteRule regex would I setup to grab this? I understand RewriteRules and the flags, putting matched results into variables, etc, I just can't figure out what regex I should write to grab the actual result.

If this is too complex for RewriteRules, then please let me know as I am struggling here.

Usually I do these in PHP and would start with:
.com/[a-zA-Z0-9-_.]+
([^/]+)/?$
Then do good 'ol substrings and checks. It's hacking it to death and I should be doing better!

I am currently going through the regular-expressions.info tutorials and am making progress, but I keep grabbing the wrong things too.

Thanks for any help you can send my way!

Update: I was able to resolve everything with a ton of help and discussed more here: Mod_Rewrite conditions help for hotlinking but allow local requests

解决方案

I need everything other than http:// www.domain.com/ or http:// domain.com/ or domain.com/:

RewriteCond %{REQUEST_URI} !^/$

But, I don't want any of these urls in the results:

RewriteCond %{REQUEST_URI} !^/some_dir/

The matched part will then be appended to:

RewriteRule ^(.*)$ /some_dir/som_subdir/some_file.php?querystring=$1 [L]

So in all it should look something like this:

RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_URI} !^/some_dir/
RewriteRule ^(.*)$ /some_dir/som_subdir/some_file.php?querystring=$1 [L]

Which will make it so when you request something like http://www.domain.com/asa45s.html, it will get internally rewritten to some_dir/som_subdir/some_file.php?querystring=asa45s.html. As for the hotlinking bit:

RewriteCond %{REQUEST_URI} ^/image_dir/
RewriteCond %{REQUEST_URI} \.(png|gif|jpe?g|bmp|ico)$ [NC]
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?domain.com/
RewriteRule ^(.*)$ /some_dir/som_subdir/some_file.php?querystring=$1 [L]

This checks that first the request is for something in the /image_dir/ directory, then that the requested resource ends with a png/gif/jpeg/bmp/ico extension, then that the HTTP referer [sic] does not start with http://www.domain.com/, https://domain.com/ or whatever combination of the 2. If all those are true, then it rewrites the request to the /some_dir/som_subdir/some_file.php file with the original URI as the querystring parameter.

这篇关于.htaccess 重定向和防止热链接的正确正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆