I have a folder mywebsite.com/wp-content/customUploads which contains hundreds of pdf files.

At this point the url to see a pdf file is for example: mywebsite.com/wp-content/customUploads/myfile.pdf

My goal is to have a url for each PDF file like: mywebsite.com/pdf-files/myfile.pdf

So if a user request the above url, the website will load the pdf file.

Is there a way to request the url:

  • mywebsite.com/pdf-files/myfile.pdf

and this url is translated to the following, so the file could be loaded correctly?

  • mywebsite.com/wp-content/customUploads/myfile.pdf
  • 1
    A URL such as that would bypass WP logic entirely, so any rewrite would have to take place outside of WP.
    – vancoder
    Commented Oct 25, 2021 at 18:30
  • Can you please be more specific? Give me an example so i can understand your suggestion. Thank you.
    – billato
    Commented Oct 25, 2021 at 18:35
  • I can't give you an example because by it's nature this is not a WordPress question. The URL you want to use ends in .pdf, so no PHP whatsoever will be engaged to access that file, therefore nothing you do in WP will help. You'll probably have to use htaccess or similar, such as here.
    – vancoder
    Commented Oct 25, 2021 at 18:42
  • Ok, so i am looking for htaccess rewrite rules or something like that.
    – billato
    Commented Oct 25, 2021 at 18:49
  • 1
    this sounds more like a redirect than a rewrite, otherwise this would be an Apache/Nginx question
    – Tom J Nowell
    Commented Oct 25, 2021 at 19:02

1 Answer 1


You can achieve this in WordPress by using custom rewrite rules.

Use this code inside theme's function.php file or inside a plugin.

// 1- Create a custom query var
    add_filter( 'query_vars', function ( $query_vars ) {
        $query_vars[] = 'my_pdf_file';
        return $query_vars;

    // 2- Add a custom rewrite rule.
    // NOTE: rewrite rules must be flushed for this rule to work.
    // You can flush rewrite it by saving permalinks from dashboard.
    add_action('init', function (){
        add_rewrite_rule('^pdf-files/([a-zA-Z0-9 _\-]+\.pdf)$','index.php?my_pdf_file=$matches[1]' );

    // 3- Find and display the PDF file when a pdf file link is visited.
    add_action('template_redirect', function () {

        $pdf_file_name = get_query_var('my_pdf_file');

        // Don't do anything if no file name is supplied
        if(! $pdf_file_name) {

        // Create the file path with the supplied name.
        $pdf_file_path = \WP_CONTENT_DIR . '/customUploads/' . $pdf_file_name;

        // Show 404 page if invalid file name is supplied
        if(! is_file($pdf_file_path)) {
            global $wp_query;

        // Read and display the PDF file
        header("Content-type: application/pdf");


Don't forget to save permalinks from "permalinks settings" page on WordPress dashboard to flush rewrite rules after using the above code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.