I'm currently using a plugin that generates a PDF for customers but the URL is way too long. For eg - https://www.mywebsite.com/wp-content/uploads/pdfuploads/666666.pdf. Here's the specific code from the plugin relating to this:

$upload = wp_upload_dir();
$upload_dir = $upload['basedir'];
$upload_dir = $upload_dir . '/pdfuploads';
if (! is_dir($upload_dir)) {
  mkdir( $upload_dir, 0755 );
  $file = fopen($upload_dir.'/index.html',"wb");
  echo fwrite($file,"Silence is golden.");

The PDF then generates in:

=> get_home_url().'/wp-content/uploads/pdfuploads/'
$attachments[0] = $upload_dir.'/pdfuploads/'

I want to shorten this URL so it reads something like - https://www.mywebsite.com/pdfs/666666.pdf. I tried adding RewriteRule ^wp-content/uploads/pdfuploads/(.*)$ /pdfs/$1 [R=301,NC,L] to htaccess but that didn't move the files over. Is there a quick fix for this via htaccess or functions? Otherwise, if I need to edit the plugin's PHP files myself, what do I need to change in the above code?

  • My current answer might help, although it doesn't actually move the file to another folder. If you want to do that, then you should provide more code, particularly the part which generates/saves the PDF file. – Sally CJ Apr 15 at 9:49
  • I just tried all of the below and it didn't move the file to the "pdfs" folder. There are numerous files involved so am not sure which code to add. The PDF link is "=> get_home_url().'/wp-content/uploads/pdfuploads/'.$voucher_options->voucherpdf_link.'.pdf', - The plugin is - wordpress.org/plugins/gift-voucher - in case that helps. – Sam Apr 15 at 10:15
  • Have you edited your .htaccess file? The rewrite rule works, right? Because to actually move the files, that's a pretty complex task and I can't help you with that. But if the rewrite rule works, then I can tell you which files you can edit. – Sally CJ Apr 15 at 10:40
  • And by "works", I mean when you visit http://example.com/pdfs/filename.pdf, you get the correct PDF file. – Sally CJ Apr 15 at 10:50
  • Yes the htaccess part worked and the links point to the 'pdfs' folder. But clicking on them results in an error since the file isn't there. I don't necessarily need to move any existing files there but any files that are created from here on, I'd like them to be saved in the 'pdfs' folder rather than the wp-content one. – Sam Apr 15 at 10:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.