I know there are articles on the subject but I can't get the htaccess rule to work. Well, it works but only if the PDF is in a specific folder.
Here's what I'm trying to do : If someone doesn't have the wordpress_logged_in cookie and they try to view a pdf or ppt, then we redirect them to the login page.
Here is my current htaccess :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteCond %{HTTP_COOKIE} !wordpress_logged_in [NC]
RewriteRule ^wp-content/uploads/.+?\.(pdf|ppt)$ wp-login.php?redirect_to=/ [R,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
It dont works.
The next one works if i'm trying to access /uploads/2022/05/blabla.pdf but not /wp-content/uploads/2022/05/blabla.pdf
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteCond %{HTTP_COOKIE} !wordpress_logged_in [NC]
RewriteRule (^|/)uploads/.+?\.(pdf|ppt)$ wp-login.php?redirect_to=/ [R,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Why If I add "^wp-content/" it dont works at all?
Edit : There is another .htaccess in /wp-content/ maybe some conflict?
# BEGIN WebP Converter
# ! --- DO NOT EDIT PREVIOUS LINE --- !
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_ACCEPT} image/webp
RewriteCond %{DOCUMENT_ROOT}/wp-content/uploads-webpc/$1.jpg.webp -f
RewriteRule (.+)\.jpg$ /wp-content/uploads-webpc/$1.jpg.webp [NC,T=image/webp,L]
RewriteCond %{HTTP_ACCEPT} image/webp
RewriteCond %{DOCUMENT_ROOT}/wp-content/uploads-webpc/$1.jpeg.webp -f
RewriteRule (.+)\.jpeg$ /wp-content/uploads-webpc/$1.jpeg.webp [NC,T=image/webp,>
RewriteCond %{HTTP_ACCEPT} image/webp
RewriteCond %{DOCUMENT_ROOT}/wp-content/uploads-webpc/$1.png.webp -f
RewriteRule (.+)\.png$ /wp-content/uploads-webpc/$1.png.webp [NC,T=image/webp,L]
</IfModule>
<IfModule mod_headers.c>
<FilesMatch "(?i)\.(jpg|jpeg|png)(\.(webp|avif))?$">
Header always set Cache-Control "private"
Header append Vary "Accept"
</FilesMatch>
</IfModule>
# ! --- DO NOT EDIT NEXT LINE --- !
# END WebP Converter