General reference request mod_rewrite

I am looking for a generic (host-independent) mod_rewrite rule set to check for HTTP_REFERER resources. I came up with the following, which seemed intuitive, but unfortunately does not work:

RewriteCond %{HTTP_REFERER} !^https?://%{HTTP_HOST}/.* # RewriteRule .* - [F] # <- or whatever 

Apparently, you cannot have a variable on either side of the comparison. So hack:

 RewriteCond %{HTTP_HOST}##%{HTTP_REFERER} !^([^#]*)##https?://\1/.* 

But wow, that’s ugly - and if you don’t know exactly what is going on, it is terribly confusing.

Is there a better (cleaner) way to write these rules?

+7
source share
1 answer

"if you don’t know exactly what is going on, this is terribly confusing"

First, congratulations on the workaround. The source check, mod_rewrite.c does not seem to do any variable interpolation of variables, so I cannot think of an alternative. As for your "confusing" point, not because we have comments? I also tidied up your regular expression (e.g. trailing. * Excessively) and used = as a delimiter to emphasize that you are doing a comparison.

It may look sticky, but your idea is close to optimal in terms of execution time.

 # # Is **HTTP_REFERER** of the form http(s)://HTTP_HOST/.... # Note that mod_rewrite only does interpolation in the teststring so this is # set up in the format AAAA=BBBB and the pattern uses a backreference (\1) to # match the corresponding elements of AAAA and BBBB # RewriteCond %{HTTP_HOST}==%{HTTP_REFERER} !^(.*?)==https?://\1/ 
+6
source

All Articles