0

i have the following code on my htaccess, it works for all users but i want deny access only to not logged in Wordpress users, anyone can help me? Thanks!

RewriteEngine on 
RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost [NC] 
RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost.*$ [NC] 
RewriteRule \.(pdf|doc|docx|odt)$ - [F]
Jakkah
  • 1
  • 1
  • 2
    You can't do this in `.htaccess` alone as you need to authenticate the user (using the same mechanism as you are using for WordPress). See [my answer](https://stackoverflow.com/a/60551857/369434) to [a related question on StackOverflow](https://stackoverflow.com/q/60544447/369434) to get the basic idea. There are numerous other questions of this nature on the [WordPress stack](https://wordpress.stackexchange.com/). – MrWhite Mar 25 '20 at 15:52
  • Also, the example you gave is not secure, as it relies on `%{HTTP_REFERER}`, which comes from the user, without any control. Anyone can access the files simply by fabricating the `Referer` header. – Esa Jokinen Mar 25 '20 at 16:39
  • @MrWhiteThanks, It's seems very well documented, i definetely move in that direction! – Jakkah Mar 25 '20 at 18:37
  • @EsaJokinen Got it! Momentaneally i simply change HTTP to HTTPS?! It works? – Jakkah Mar 25 '20 at 18:37
  • Nope. It would be still tamperable. – Esa Jokinen Mar 25 '20 at 18:55

1 Answers1

0

I read some related question to mine on StackOverflow and following the idea of MrWhite I found a solution to the problem for my specific file extension.

.htaccess

<IfModule mod_rewrite.c>

RewriteCond %{REQUEST_URI} \.(pdf|doc|docx|odt)$ [NC]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^wp-content/uploads/(.*)$ dl-file.php?file=$1 [QSA,L]

</IfModule>

dl-file.php (public html folder)

there is related question and code author in the php comments below

<?php
/*
 * dl-file.php
 *
 * Protect uploaded files with login.
 * 
 * @link http://wordpress.stackexchange.com/questions/37144/protect-wordpress-uploads-if-user-is-not-logged-in
 * @link https://gist.github.com/hakre/1552239
 *
 * @author Nathan Parikh <https://ndzynes.com/>
 * @license GPL-3.0+
 * 
 */

ob_start();
require_once('wp-load.php');
require_once ABSPATH . WPINC . '/formatting.php';
require_once ABSPATH . WPINC . '/capabilities.php';
require_once ABSPATH . WPINC . '/user.php';
require_once ABSPATH . WPINC . '/meta.php';
require_once ABSPATH . WPINC . '/post.php';
require_once ABSPATH . WPINC . '/pluggable.php';
wp_cookie_constants();
$discard = ob_get_clean();

is_user_logged_in() ||  auth_redirect();


list($basedir) = array_values(array_intersect_key(wp_upload_dir(), array('basedir' => 1)))+array(NULL);

$file = rtrim($basedir, '/') . '/' . (isset($_GET['file']) ? $_GET['file'] : '');
$file = realpath($file);

if ($file === FALSE || !$basedir || !is_file($file)) {
    status_header(404);
    die('404 &#8212; File not found.');
}

if (strpos($file, $basedir) !== 0) {
    status_header(403);
    die('403 &#8212; Access denied.');
}

$mime = wp_check_filetype($file);
if( false === $mime[ 'type' ] && function_exists( 'mime_content_type' ) )
    $mime[ 'type' ] = mime_content_type( $file );

if( $mime[ 'type' ] )
    $mimetype = $mime[ 'type' ];
else
    $mimetype = 'image/' . substr( $file, strrpos( $file, '.' ) + 1 );

header( 'Content-Type: ' . $mimetype ); // always send this
if ( false === strpos( $_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS' ) )
    header( 'Content-Length: ' . filesize( $file ) );

$last_modified = gmdate( 'D, d M Y H:i:s', filemtime( $file ) );
$etag = '"' . md5( $last_modified ) . '"';
header( "Last-Modified: $last_modified GMT" );
header( 'ETag: ' . $etag );
header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + 100000000 ) . ' GMT' );

// Support for Conditional GET
$client_etag = isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) ? stripslashes( $_SERVER['HTTP_IF_NONE_MATCH'] ) : false;

if( ! isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) )
    $_SERVER['HTTP_IF_MODIFIED_SINCE'] = false;

$client_last_modified = trim( $_SERVER['HTTP_IF_MODIFIED_SINCE'] );
// If string is empty, return 0. If not, attempt to parse into a timestamp
$client_modified_timestamp = $client_last_modified ? strtotime( $client_last_modified ) : 0;

// Make a timestamp for our most recent modification...
$modified_timestamp = strtotime($last_modified);

if ( ( $client_last_modified && $client_etag )
    ? ( ( $client_modified_timestamp >= $modified_timestamp) && ( $client_etag == $etag ) )
    : ( ( $client_modified_timestamp >= $modified_timestamp) || ( $client_etag == $etag ) )
    ) {
    status_header( 304 );
    exit;
}

// If we made it this far, just serve the file
readfile( $file );
Jakkah
  • 1
  • 1