Search code examples
wordpressapache.htaccess

Redirect people who are not logged and trying to view a pdf


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

Solution

  • Edit : There is another .htaccess in /wp-content/ maybe some conflict?

    The mod_rewrite directives in /wp-content/.htaccess will completely override the mod_rewrite directives in the parent .htaccess file. The mod_rewrite directives (and your rewrite) in the parent .htaccess file are not even processed.

    You need to either (in order of ease to implement):

    • Repeat the rule at the top of the /wp-content/.htaccess file (before the # BEGIN WebP Converter line) - although it will need to be adjusted accordingly. For example:

      RewriteCond %{HTTP_COOKIE} !wordpress_logged_in [NC]
      RewriteRule ^uploads/.+\.(pdf|ppt)$ /wp-login.php?redirect_to=/ [R,L]
      

      Note the RewriteRule pattern now starts ^uploads, not ^wp-content and the substitution string is prefixed with a slash.

      No need for a non-greedy quantifier. .+ (rather than .+?) is sufficient.

    • Move the contents of the /wp-content/.htaccess file into the parent config - making the necessary adjustments. (Although this might also require customisation of the "WebP plugin".)

    • Enable mod_rewrite inheritance in the /wp-content/.htaccess (or root .htaccess) file. But this does complicate matters and the directives will need to be rewritten to account for this.