title: .htaccess - Check Referer Matches Host Without Hard Coding?
body: <p>I would like to do the typical thing where you make sure a referrer header matches your host, using htaccess. However I would like to do this without hard coding the domain name, so the htaccess code can easily be reused across many many sites.</p>
  
  <p>I know this is the typical way to do it. Imagine I have one main server at WWW.example.com, and an image server at IMG.example.com:</p>
  
  <pre><code>RewriteCond %{HTTP_HOST} !^(www\.)
  RewriteCond %{HTTP_REFERER} !^$
  RewriteCond %{HTTP_REFERER} !https?://(([^\.]+)\.)?example\.com/    [NC]
  RewriteRule DO SOMETHING HERE [L]
  </code></pre>
  
  <p>But how can I somehow do this without hardcoding 'example.com' into the Rewrite condition? I was thinking of trying to use %{HTTP_HOST} inside the referrer rewrite condition somehow, but I'm not sure how to match just the last/domain part and bring it inside. Maybe there is some way I can match the host, put the results into a variable, then be able to use that variable on the next RewriteCond line?</p>
  
  <p>I'm a little lost. Thanks for any help!</p>