I want to restrict access to certain pages based upon where the visitor came from. For example, if someone clicks a link on a page that is hosted on www.mydomain.com then they can see the file.

But if someone tries to access that file by entering its url into the goto window, then they get sent elsewhere. Basically if the referring url is either blank, or is not www.mydomain.com, then the visitor is redirected.

I have a script that works, but I must input the name of the domain it is running on to make it work, like this:

RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?mydomain\.com [NC]

I want to be able to copy and paste the code onto any server and have it work without having to edit the name of the domain.

This is what I am using, but it doesn't work:

RewriteCond %{HTTP_REFERER} ^!{SERVER_NAME} [NC]

Is there any way to make this work?