I’m saving some pdfs into a folder with mpdf, the urls of pdfs are like this:
https://example.com/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.com/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.com/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.com/invoice_([0-9]+).pdf$', '/wp-content/themes/mysitetheme/includes/mpdf/invoice.php?id=$1', 'top' );
} );
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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;
}
Method 2
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.com/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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0