Sample Header Ad - 728x90

allow display and or execution of web files but not direct url access using htaccess

-1 votes
2 answers
141 views
**What I want to Achieve is:** I want to be able to display the (mp4 | jpg | ico) file types and execute the (js | css) file types on the browser only on my domain but I dont want them to directly access those files thru typing the URL on browser address bar. **What I expect and done is** Being familiar with PHP $_SERVER['HTTP_REFERER'], I get a URL like http://domainname.xyz/main.php but I do not know if the same goes with .htaccess. Because with .htaccess, the HTTP_REFERER, uses a regex pattern like ^https?://(www.)?domainname\.xyz/.*$ but if I use the pattern the URL as https?://(www.)?domainname\.xyz/main.php that I can hard code that exact URL on .htaccess. Such URL causes error thus I followed the code from sites and incorporated those to my script. I am not expert on htaccess regex. **So I have the .htaccess below as researched result and followed from various sites:** RewriteEngine On RewriteCond %{HTTP_REFERER} !^https?://(www.)?domainname\.xyz/.*$ [NC] RewriteRule \.(jpg|js|css|mp4)$ - [NC,F,L] Also I tried using the code below followed from various sites too: SetEnvIf Referer "!^https?://(www.)?domainname\.xyz/.*$" localreferer Require env localreferer **What is currently happening with the above code is:** With the above .htaccess scripts, only the mp4 file types is displayed on the browser and cannot be directly access using thru the URL on the browser address bar and showing 403 Forbidden error. All other files types are displayed/executed on the browser and they are also directly accessed on the address bar using the URL. **Help** I seek your help on this because I am not an expert on htaccess, regex. I have researched for days now and still no fix. With your help, it will add to my knowledge.
Asked by genesis lubrigas (19 rep)
Sep 12, 2023, 05:38 AM
Last activity: Sep 15, 2023, 09:09 AM