![](/img/trans.png)
[英]How to redirect URL with htaccess (RewriteRule) and prevent direct access
[英].htaccess redirect direct access
我試圖阻止用戶直接訪問我網站上的文件。 基本上我有一個位於/images/header.jpg的圖像
我正在嘗試使用.htaccess將所有嘗試直接訪問該文件“http://www.domain.com/images/header.jpg”轉發到我的404頁面但我希望從我的調用時仍然加載圖像源代碼。 使情況復雜化的是我實際上將header.jpg重定向到另一個圖像文件夾。
所以最終期望的效果是:
用戶加載頁面 - > header.jpg被調用並重定向到new_header.jpg用戶嘗試鏈接header.jpg從他們的網站 - >他們被重定向到我的404頁面。
我嘗試了一些條件,但未能達到預期的效果......
RewriteCond %{REQUEST_URI} ^/images
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?domain.com [NC]
RewriteRule ^(.*\header.jpg(/.*)?)$ new_header.jpg [L,QSA]
我認為這樣的事情對你有用:
RewriteEngine On
#Redirect all request for things in /images that don't come from website to 404 page (assumed to be /404 in this case) - Prevents hotlinking
RewriteCond %{REQUEST_URI} ^/images
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?domain\.com [NC]
RewriteRule ^.*$ /404 [L,R=404]
#Redirect requests for /images/header.jpg to new_header.jpg image
RewriteRule ^/?images/header.jpg /path/to/other/images/new_header.jpg [L]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.