Hotlink protection is a technique used to prevent other sites from directly linking to files (as images) on your site, which can use up your hosting bandwidth.
Table of Contents
More About Hotlink Protection
Implementation: Can be implemented via .htaccess or control panel settings.
Benefits: Saves bandwidth and prevents unauthorized use of content.
How it Works: Blocks requests that come from external sites.
Considerations: Important to configure correctly to avoid blocking legitimate traffic.
Here’s how to activate hotlink protection using .htaccess
and in cPanel:
How to Activate Hotlink Protection using .htaccess
- Access Your Website’s .htaccess File:
- Use an FTP client or your web hosting control panel’s file manager to access your website’s root directory.
- Locate the
.htaccess
file or create one if it doesn’t exist.
- Edit the .htaccess File:
- Open the
.htaccess
file using a text editor.
- Open the
- Add the Hotlink Protection Code:
- Insert the following code into your
.htaccess
file:RewriteEngine on RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourwebsite\.com [NC] RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]
- Replace
yourwebsite\.com
with your actual domain name. - You can add or remove file extensions (e.g.,
jpg|jpeg|png|gif
) to protect different types of files.
- Insert the following code into your
- Save and Upload:
- Save the changes to your
.htaccess
file and upload it back to your website’s root directory.
- Save the changes to your
How to Activate Hotlink Protection in cPanel
- Log in to cPanel:
- Access your web hosting control panel (cPanel) using your credentials. Here’s how to login to cPanel.
- Locate the “Hotlink Protection” Option:
- In the cPanel dashboard, search for “Hotlink Protection” in the search bar or navigate to the “Security” section.
- Enable Hotlink Protection:
- Click on “Hotlink Protection” to access the tool.
- Enable hotlink protection by checking the box or toggle switch, depending on your cPanel version.
- Define Protected Extensions:
- In the “URLs to allow access” section, add your domain (e.g.,
yourwebsite.com
) to the list of allowed domains. - Below that, specify the file extensions you want to protect (e.g., jpg, jpeg, png, gif).
- In the “URLs to allow access” section, add your domain (e.g.,
- Configure Redirect Option (Optional):
- You can choose whether to redirect hotlinked requests to another URL or display a specific image.
- Configure this option according to your preference.
- Save Settings:
- Click the “Submit” or “Save” button to activate hotlink protection.
- Test Hotlink Protection:
- To test whether hotlink protection is working, try to access an image on your site from another domain. It should be blocked or redirected as per your configuration.
How to Activate Hotlink Protection in Cloudflare
To activate Hotlink Protection in Cloudflare, you can use Cloudflare’s Page Rules feature. Page Rules allow you to set specific actions for different URLs on your website, including enabling Hotlink Protection. Here’s how to do it:
- Log in to your Cloudflare account:
- Go to the Cloudflare website (https://www.cloudflare.com/) and log in to your account.
- Select Your Domain:
- From the Cloudflare dashboard, select the domain for which you want to activate Hotlink Protection.
- Go to Page Rules:
- In the left-hand menu, click on the “Page Rules” tab.
- Create a Page Rule:
- Click the “Create Page Rule” button to create a new rule.
- Set the URL Pattern:
- In the “If the URL matches” field, enter the URL pattern for which you want to enable Hotlink Protection. You can use wildcard characters like
*
to match multiple URLs. For example, to protect all images, you can use*yourwebsite.com/*.jpg
.
- In the “If the URL matches” field, enter the URL pattern for which you want to enable Hotlink Protection. You can use wildcard characters like
- Add a Setting:
- Click on the “Add a Setting” button.
- Select “Cache Level” and “Bypass”:
- From the dropdown menu, choose “Cache Level” and select “Bypass.” This ensures that Cloudflare will not cache the hotlinked content.
- Add Another Setting:
- Click on the “Add a Setting” button again.
- Select “Browser Cache TTL” and “Respect Existing Headers”:
- Choose “Browser Cache TTL” from the dropdown menu and select “Respect Existing Headers.” This setting respects the existing cache headers for the protected content.
- Save and Deploy:
- Click the “Save and Deploy” button to activate the Page Rule.
Now, Cloudflare will apply Hotlink Protection to the specified URLs, ensuring that only authorized websites can access and display your content. Any attempts to hotlink to your images or files from unauthorized domains will be prevented.
Hotlink protection helps preserve your website’s bandwidth and resources by preventing other websites from embedding or directly linking to your files. It’s a useful security measure for website owners.