How to write a rule for pdf file that exist in a folder

How do I write a rule for files that already exist in a folder to point to a different location. In example, for rewrite any request to a pdf file in the wp-content/uploads/ folder to a page download/?q=$1.

I have seen in SO a related question for moved pdf files with the following

Read More
# checks that the request is for a file that *does not* exist
RewriteCond %{REQUEST_FILENAME} !-f  
# make sure we've not already redirected to the uploads directory 
# (in case it doesn't exist in there either)
RewriteCond %{REQUEST_URI} !^/wp-content/uploads/
# the rule matches against either pdf, zip or xls.
RewriteRule ([^/]*.(pdf|zip|xls))$ /wp-content/uploads/$1 [NC,L,R=301]

But I want the opposite effect. Make the redirection for my existing files and keep a 404 not found for non-existing files.

I have tried different approach without no luck. Wondering if the default .htaccess file of WordPress within the root folder if interfering with this one. How to rewrite all requests of a pdf to a custom page?

EDIT:

The .htaccess file is located in the wp-content/uploadsfolder.

Regarding the WordPress Rewrite API I already have the following function in a custom plugin:

function add_rewrite_rules($aRules) {
    $new_rules = array(
    'exams/([^/]+)/([^/]+)/?$' => 'index.php?pagename=exams&type=$matches[1]&city=$matches[2]',
    'exams/([^/]+)/([^/]+)/subject/([^/]+)/?$' => 'index.php?pagename=exams&type=$matches[1]&city=$matches[2]&subject=$matches[3]'
        );

    return $aRules + $new_rules;
}

From my understanding, these functions allows us to specify additional rewrite rules for WordPress as described in the Rewrite API/add rewrite rule documentation. So, It should not affect the content of the rules specified in the .htaccess file.

Related posts

2 comments

  1. Assuming your WP is installed on the root of your domain, it’d be:

    RewriteCond %{REQUEST_FILENAME} -f
    RewriteCond %{REQUEST_URI} /wp-content/uploads/
    RewriteRule ([^/]+.pdf)$ /download.php?q=$1 [NC,L,R=301]
    

    The first line (removing the ! from your example code) matches only existing files, so if the PDF file doesn’t exist you’ll get the standard 404.

    The second condition restricts the matches to only existing files on the /wp-content/uploads/ folder, so http://example.com/example.pdf won’t be redirected.

    Finally, the RewriteRule (affected by the two precedent conditions) redirects any existing file on your uploads folder ending with a .pdf extension to /downloads.php passing the filename as q parameter.

    So, http://example.com/wp-content/uploads/2016/04/filename.pdf will be redirected to http://example.com/download.php?q=filename.pdf if filename.pdf exists in the /wp-content/uploads/2016/04/ folder.

    I’ve checked this solution in a local dummy WP install and it works, so I hope this helps.


    Edit: If you have an .htaccess in your uploads folder, only with this should be enough:

    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ([^/]+.pdf)$ /download.php?q=$1 [NC,L,R=301]
    

    Have you tried what’s been suggested on comments (putting some garbage in your .htaccess to check if it’s enabled)?

    Another shoot would be to move the rules to the root .htaccess

  2. You can use the following as first rule in your /wp-content/uploads/.htaccess :

    RewriteEngine on
    
    RewriteRule ^([^.]+.pdf)$ /download/?q=$1 [NC,L]
    

    This will internally redirect /wp-content/uploads/foo.pdf to /download/?q=foo.pdf .

    If you want to externally redirect /wp-content/uploads/foo.pdf to /download/?q=foo.pdf (changing the url bar from old location to new location) use the full url in Rewrite target eg http://example.com/download/?q=foo.pdf or add the R Redirect flag [NC,L,R]

Comments are closed.