can you help me with little problem.

I need to find a solution, how to redirect user to login page when he wants to download file over link in post content, so, I have some files and I want them to be available only for users that are logged in, otherwise they should be redirected on login page, for example I want to forbid access (download) for all files from wp-content folder, keep in mind that I have several of those folders in the root, I tried with .htaccess file file but its not working

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(www\.)?brt\.keezst\.com/ [NC]
RewriteCond %{REQUEST_URI} !hotlink\.(gif|png|jpg|doc|xls|pdf|html|htm|xlsx|docx|mp4|mov) [NC]
RewriteCond %{HTTP_COOKIE} !^.*wordpress_logged_in.*$ [NC]
RewriteRule .*\.(gif|png|jpg|doc|xls|pdf|html|htm|xlsx|docx|mp4|mov)$ http://brt.keezst.com/ [NC]

3 Answers
3

I wouldn’t use htaccess for that.

What I would do is make a ‘download page’ template and use it to ‘serve’ the files.

By doing that, I can do a check with is_user_logged_in() Codex and redirect with wp_login_url() Codex

Just do:

if ( is_user_logged_in() ) :
//file link
else :
    wp_login_url();
endif;

I think it’s easier and safer to do that. Besides, you can even make that into a plugin later and use it on another projects or keep using it if you change your site template.

Leave a Reply

Your email address will not be published. Required fields are marked *