Protect media files WordPress multisite

I’m using a multisite with WordPress as a private network using the Private Only plugin for this.

But the media files from my wp-content map are not protected and are indexed by Google, although I denied indexing.

I’m searching for a piece of code which redirects people who are not logged in when accessing media files through Google.

Read More

My domain structure is as follows:

https://intranet.website.com/subsite1/
https://intranet.website.com/subsite2/

I have tried the following piece of code in my .htaccess and it did work but it broke the images on my subsites.

RewriteCond %{HTTP_HOST} ^intranet.website.com 
RewriteCond %{SCRIPT_FILENAME} ^([_0-9a-zA-Z-]+/)?files/(.+) 
RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) - [L]

Current .htaccess:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^intranet.website.com$
RewriteRule /files/.+?.(pdf|docx|xls|ppt)$ - [NC,F]

RewriteEngine On
RewriteRule ^index.php$ - [L]

# uploaded files
RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]

# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*.php)$ $2 [L]
RewriteRule . index.php [L]

# directory browsing
Options All -Indexes

Related posts

Leave a Reply

1 comment

  1. You can use this rule:

    RewriteCond %{HTTP_HOST} ^intranet.website.com$
    RewriteRule /files/.+?.(pdf|docx|xls|ppt)$ - [NC,F]
    

    Full .htaccess:

    RewriteEngine On
    RewriteBase /
    
    RewriteRule (^|/)files/.+?.(pdf|docx|xls|ppt)$ - [NC,F]
    
    RewriteRule ^index.php$ - [L]
    
    # uploaded files
    RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]
    
    # add a trailing slash to /wp-admin
    RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
    RewriteRule ^([_0-9a-zA-Z-]+/)?(.*.php)$ $2 [L]
    RewriteRule . index.php [L]
    
    # directory browsing
    Options All -Indexes