Search code examples
apache.htaccessmod-rewriteapache2

What does @ symbol mean in mod_rewrite rules


I was looking for a way to prevent my PDF files to be accessed from direct URL on my website and I found theses htaccess rules :

RewriteEngine On
RewriteCond %{HTTP_HOST}@@%{HTTP_REFERER} !^([^@]*)@@http?://\1/.*
RewriteRule .*\.pdf [NC,F]

Even though it seems to work perfectly, I don't really understand what these @ symbols mean on the RewriteCond rule. I've some basics with regex but I haven't found anything related to these on apache and regex docs, and the article where I found the rules doesn't provide any info.

Any ideas?


Solution

  • Short answer: Its basically a segregator between values of HTTP_HOST and %{HTTP_REFERER}. To match their values while performing condition check in RewriteCond directive.

    Explained answer: Now why we are putting these @@ characters as a segregator between 2 apache variables. Its simple whenever we want to compare if 2 values are EQUAL or SAME then we use it, because this helps us to catch value in capturing group and then later if back reference value used in condition is NOT same then our condition will fail.

    Now come on to this current scenario:

    let's say our domain name is: www.example.com and HTTP_REFERER value is: http://www.example.com/en-US/JavaScript

    Then what %{HTTP_HOST}@@%{HTTP_REFERER} will do is: it will make value as: www.example.com@@http://www.example.com/en-US/JavaScript

    Now come on the right side of Cond line: !^([^@]*)@@http?://\1/.*

    You see capturing group will have value as www.example.com and when we are using it as \1 in http?://\1 its actually checking if URL is http://www.example.com/.* or not. if its NOT EQUAL then go ahead with the request of URI.

    Basically why we are doing this because there is NO direct way to check if 2 values are equal or not in URI.

    Suggestion on improving your Rules:

    RewriteEngine On
    RewriteCond %{HTTP_HOST}@@%{HTTP_REFERER} !^([^@]*)@@http?://\1/.*
    RewriteRule .*\.pdf/?$ - [NC,F]