If you like DNray Forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...

 

Protect from hotlink on virtual webhosting

Started by HarshMehra, Dec 21, 2022, 08:05 AM

Previous topic - Next topic

HarshMehraTopic starter

The website is hosted on WordPress on a virtual server, which does not allow editing of the configuration file. The purpose of this configuration is to protect from hotlink using a file called .htaccess. The usual code for this purpose does not work in this case due to the nginx+apache bundle that is used by the host. Static files with extensions like jpg, jpeg, gif, png, and css are given via nginx and are not processed through apache. This means the directives in the .htaccess file won't work either. To solve this problem, links to files with different extensions or files that are not on the server must be created. This way, the request will be transmitted to the Apache web server, and directives can be used in the .htaccess file to handle these requests.

To explain this in simpler terms, when someone tries to access a static file like an image or a CSS file, it is served by nginx, not Apache. So any rules in the .htaccess file won't work. To make sure the rules work, we need to somehow force Apache to handle these requests instead of nginx. This can be done by creating dummy files with different extensions that are actually symbolic links to the original files. For example, if we have an image file called "image.jpg", we can create a symbolic link to it called "image.php" and Apache will handle the request for "image.php". In this way, we can use the .htaccess file to apply our rules.

Unfortunately, there is a solution on the Internet, but it requires editing the web server configuration file, which is not possible with the current setup. There is another solution on the network that involves replacing the indigenous index.php file, but it did not provide any results. It's possible that there are other solutions to this problem, so if anyone knows of any, please share them.

<?php
ob_start
();
/**
 * Front to the WordPress application. This file doesn't do anything, but loads
 * wp-blog-header.php which does and tells WordPress to load the theme.
 *
 * @package WordPress
 */

/**
 * Tells WordPress to load the WordPress theme and output it.
 *
 * @var bool
 */
define('WP_USE_THEMES'true);

/** Loads the WordPress Environment and Template */
require('./wp-blog-header.php');
$searches = array(
      
'/wp-content/uploads/2013/04/',
      
'/wp-content/uploads/2013/05/',
      
'/wp-content/uploads/2013/06/'
      
);
      
$replaces = array(
      
'/images/04/'
      
'/images/05/',
      
'/images/06/'
      );
      
$buffer str_ireplace($searches$replacesob_get_contents());
ob_end_clean();
echo 
$buffer;
  •  


lamnzxzfd

Placing Nginx at the front-end makes it irrelevant what modifications are made to the static elements (pictures, styles, scripts) in .htaccess. Apache will never receive the request and thus cannot process .htaccess rules.
This is why Nginx is positioned at the front-end.

For hotlink protection in Nginx, a single rule in your virtual host is all that's needed. If your hosting provider doesn't allow you to edit your virtual host config (or at least part of it), then switching to a different hosting provider may be your only option.
An alternative solution would be to use a CDN, as suggested by Mikhail. Essentially this involves another instance of Nginx at the front-end with editable configuration, unlike your hosting provider.
  •  

halley_pham

If you're using nginx+apache in your web hosting, then switching to a different hosting provider may be necessary. This is because requests are first routed through nginx for static elements, while non-static elements are proxied to apache. Consequently, .htaccess won't be of any use.
Consider implementing a hotlink protection through a CDN instead.
  •  

agelinajohly

It seems like you're trying to find a solution for configuring the .htaccess file on a WordPress website that is hosted on a virtual server and uses the nginx+apache bundle. Since static files with certain extensions are served by nginx instead of Apache, the usual .htaccess rules won't work.

One possible solution is to create symbolic links to the original files with different extensions, forcing Apache to handle the requests instead of nginx. For example, if you have an image file called "image.jpg", you can create a symbolic link called "image.php" and Apache will handle the request for "image.php". This allows you to use the .htaccess file to apply your desired rules.

However, it seems that you're unable to edit the web server configuration file or replace the indigenous index.php file, which limits your options. If anyone knows of alternative solutions to this problem, it would be great if they could share them with you.

The code you provided appears to be the front controller for the WordPress application, which loads the WordPress environment and template. It also includes some code that replaces specific file paths in the output buffer before displaying it.


Protecting your website from hotlinking on virtual web hosting involves implementing measures to prevent other websites from directly linking to your site's images, videos, or other static content. This helps to ensure that your server resources are not used by external sites without your permission.

Here are a few common methods you can use to protect against hotlinking:

1. .htaccess Method: If you have access to the web server configuration or the ability to edit the .htaccess file, you can add rules to deny direct access to your files. For example, you can use the following code in your .htaccess file to block hotlinking for image files:
```
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourdomain.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]
```
This code checks the HTTP referer header of incoming requests and blocks all requests for image files that don't originate from your own domain.

2. Content Delivery Networks (CDNs): CDNs help optimize and protect your website by caching your static content and serving it from distributed servers worldwide. Many CDNs offer hotlink protection features, allowing you to specify which domains are allowed to access your content.

3. Watermarking or Image Obfuscation: Another approach is to add visible watermarks or overlay logos onto your images, making it less desirable for others to hotlink them. You can also consider using image obfuscation techniques such as adding unique URLs or query parameters to your image URLs, which makes it harder for others to link directly to your images.

4. Use Plugins or Security Modules: If you're using a content management system like WordPress, there might be plugins or security modules available that provide hotlink protection. These tools typically offer various options for controlling hotlinking and can simplify the process of implementing and managing the protection measures.
  •  


If you like DNray forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...