admin管理员组

文章数量:1316356

I'm saving some pdfs into a folder with mpdf, the urls of pdfs are like this:

.pdf

I want that if someone open this url it will show a shorter version like this:

.pdf

how can I obtain this result using add_rewrite_rule() and apache web server?

UPDATE as suggested I changed the code that generates pdfs in a way that are not stored in a local folder, but are generated everytime when visiting the url with a specified id parameter like this

.pdf

so now the correct rewrite rule is

/**
 * Rewrite rules
 */
add_action( 'init',  function() {
    add_rewrite_rule( '^example/invoice_([0-9]+).pdf$', '/wp-content/themes/mysitetheme/includes/mpdf/invoice.php?id=$1', 'top' );
} );

I'm saving some pdfs into a folder with mpdf, the urls of pdfs are like this:

https://example/wp-content/themes/mysitetheme/invoices/invoice_8937.pdf

I want that if someone open this url it will show a shorter version like this:

https://example/invoice_8937.pdf

how can I obtain this result using add_rewrite_rule() and apache web server?

UPDATE as suggested I changed the code that generates pdfs in a way that are not stored in a local folder, but are generated everytime when visiting the url with a specified id parameter like this

https://example/wp-content/themes/mysitetheme/includes/mpdf/invoice?id=8937.pdf

so now the correct rewrite rule is

/**
 * Rewrite rules
 */
add_action( 'init',  function() {
    add_rewrite_rule( '^example/invoice_([0-9]+).pdf$', '/wp-content/themes/mysitetheme/includes/mpdf/invoice.php?id=$1', 'top' );
} );
Share Improve this question edited Nov 10, 2020 at 9:51 silvered.dragon asked Nov 9, 2020 at 23:57 silvered.dragonsilvered.dragon 1134 bronze badges 2
  • 1 I would advise against storing uploaded invoices in your theme folder for security reasons. Also, what is mpdf? – Tom J Nowell Commented Nov 10, 2020 at 1:03
  • mpdf is a php library that you can use for create a pdf file using html + css. About the storing of invoices, you are right, I updated the code in a way that the pdf will be generated every time without saving to a local folder. – silvered.dragon Commented Nov 10, 2020 at 9:45
Add a comment  | 

2 Answers 2

Reset to default 1

ok it was easy, the problem was that to match the left side pattern you have to use $1 not $matches[1], this is the solution

/**
 * Rewrite rules
 */
add_action( 'init',  function() {
    add_rewrite_rule( '^invoice_([0-9]+).pdf$', '/wp-content/themes/mysitetheme/invoices/invoice_$1.pdf', 'top' );
} );

UPDATE

From the suggestions received in the comments, it is now clear to me that it is not convenient to use rewrite rules for pages inserted in the wordpress folder without being part of the wordpress core itself, so the suitable solution is to generate virtual pages through the use of add_query_var and include a virtual template to be called when this new query variable is requested through index.php. So the correct code is this:

// Here I define my new query var and the related rewrite rules    
add_action( 'init', 'virtual_pages_rewrite', 99 );
    function virtual_pages_rewrite() {
        global $wp;
        $wp->add_query_var( 'invoice' );
        add_rewrite_rule( '^invoice_([0-9]+).pdf$', 'index.php?invoice=$matches[1]', 'top' );
    }

    // This part is just to prevent slashes at the end of the url
    add_filter( 'redirect_canonical', 'virtual_pages_prevent_slash' );
    function virtual_pages_prevent_slash( $redirect ) {
        if ( get_query_var( 'invoice' ) ) {
            return false;
        } return $redirect;
    }
    // Here I call my content when the new query var is called
    add_action( 'template_include', 'virtual_pages_content');
    function virtual_pages_content( $template ) {
        $fattura = get_query_var( 'fattura' );
        if ( !empty( $fattura) ) {
            include get_template_directory().'/includes/mpdf/invoice.php';
            die;
        }
        return $template;
    }

WP rewrite rules are not for mapping URLs on to arbitrary files and paths.

To do this, you will need either a HTAccess rule, or Nginx config rules. WordPress rewrite rules are not intended for this.

Originally, WP permalinks took the form example/index.php?post=123, but then pretty permalinks were added, they took pretty URLs such as /post/123 and matched them to ugly permalinks index.php?post=123. This is the purpose of WP rewrite rules, to turn pretty URLs into ugly URLs with query variables that get passed to WP_Query to create the main post loop and figure out which template to load.

A WP rewrite rule consists of:

  • A regular expression that matches a URL
  • the query variables that expression extracted in the form index.php?var=value

You cannot pass arbitrary files and folders to as the second parameter when adding a rewrite rule. It is not a generic rewrite system. For that you want Apache's mod_rewrite.

Additionally, it is very bad practice to make direct requests to PHP files in WordPress themes, and a big security risk. WP is a CMS let WP handle the request and hook in to handle it in your plugin/theme.

Alternatively, add a new query var named mpdf, look for it on init, then load your MPDF script in PHP rather than doing it directly via a browser request. This would let you use WP rewrite rules.

本文标签: url rewritingrewrite rule generated with mpdf to a shorter version