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

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

问题描述

我张贴这在Reddit上(R / learnprogramming),有人有PM'd我,叫我到这里来,所以我在这里!

我一直在努力学习正则表达式的,我吮吸他们还在。我认真难以把握的模式匹配。我在其他OOP语言固体,所以我想我会学习正则表达式,它只是逃避我。

我已经下载EditPad专业,所以我可以修炼成 HTTP:// WWW。定期-EX pressions.info / tutorial.html 建议。我可以得到的前pressions匹配散装的文字,但我试图解析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

但是,我不希望在任何结果这些URL:

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

,我需要prevent盗链在image_dir图片:

HTTP:// www.domain.com/image_dir/some_dir/some_subdir/some_image.jpg(或PNG,GIF等)

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

那么,重写规则的正则表达式将我的设置,以抓住这个?我理解的规则集和标志,把匹配的结果为变量,等等,我实在不明白什么正则表达式我应该写抢实际结果。

如果这是对的RewriteRules过于复杂,那么请让我知道,因为我在这里挣扎。

通常我在PHP做这些,并会开始:
.COM / [A-ZA-Z0-9 -_] +
([^ /] +)/ $
然后做的好'醇子和检查。它是盗号的死刑,我应该做的更好!

我目前打算通过定期-EX pressions.info教程和正在取得进展,但我一直抓错东西了。

感谢您的帮助,您可以发送我的方式!

解决方案
  

我需要的一切其他的不是http:// www.domain.com/或http:// domain.com/或domain.com/:

 的RewriteCond%{REQUEST_URI}!^ / $
 

  

不过,我不希望在任何结果这些URL:

 的RewriteCond%{REQUEST_URI}!^ / some_dir /
 

  

匹配的部分将被追加到:

 重写规则^(。*)$ /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?查询字符串= 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的查询字符串参数。

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!

解决方案

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重定向和热链接prevention的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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