问题
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
回答1:
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.
来源:https://stackoverflow.com/questions/12394494/proper-regex-for-htaccess-redirect-and-prevention-of-hotlinks