Title: Using WordPress rewrite rules
Last modified: August 21, 2016

---

# Using WordPress rewrite rules

 *  [Muxh](https://wordpress.org/support/users/caciocode/)
 * (@caciocode)
 * [12 years, 7 months ago](https://wordpress.org/support/topic/using-wordpress-rewrite-rules/)
 * Hi all,
 * This is driving me insane! Hope someone experienced will help me with this. I
   don’t have a lot of experienced in WordPress. I have read many articles and threads
   in other forums, I think posting here might speed up things.
 * I am trying to write a pdf plugin for a site that should return a pdf file for
   a post when the post url has /format/pdf at the end, such that:
 * [http://worksheets.mathematicsi.com/indices-worksheet/format/pdf](http://worksheets.mathematicsi.com/indices-worksheet/format/pdf)
 * …for example returns;
 * [http://worksheets.mathematicsi.com/indices-worksheet/?format=pdf](http://worksheets.mathematicsi.com/indices-worksheet/?format=pdf)
 * I almost did it but each time I visit the url ( POST/format/pdf ) WordPress redirects
   to the post url instead. I was wondering whether someone might help me with a
   proper code. It is very simple but I don’t know how WordPress executes everything
   in the background. I also tried: remove_filter(‘template_redirect’, ‘redirect_canonical’);
   but that returns a 404 error.
 * I am fine with other functions of the project it is just getting the rewrite 
   rule POST/format/pdf to point to: POST/?format=pdf
 * I also tried to get the rewrite rule variable/’query’ but was unable to. I think
   I can’t just use $_GET, I have to register them.
 * Thanks.

Viewing 1 replies (of 1 total)

 *  Thread Starter [Muxh](https://wordpress.org/support/users/caciocode/)
 * (@caciocode)
 * [12 years, 7 months ago](https://wordpress.org/support/topic/using-wordpress-rewrite-rules/#post-4284193)
 * This is my code:
 *     ```
       function add_my_rule() {
           global $wp;
       	add_rewrite_rule('(.?.+?)/format(/[0-9]+)?/?$','index.php?pagename=$matches[1]&format=$matches[3]','top');
           /*global $wp_rewrite;
           $wp_rewrite->flush_rules();*/
       }
       add_action('init', 'add_my_rule');
   
       function add_custom_page_variables( $public_query_vars ) {
       $public_query_vars[] = 'format';
       $public_query_vars[] = 'pagename';
   
       return $public_query_vars;
   
       } // End add_custom_page_variables()
       add_filter( 'query_vars', 'add_custom_page_variables' );
       ```
   
 * I do flush the rewrite rules each time the plugin is enabled with
    flush_rewrite_rules();
 * …and the rules seem to be registering fine with WordPress, just not working the
   way I would like them to.

Viewing 1 replies (of 1 total)

The topic ‘Using WordPress rewrite rules’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 1 participant
 * Last reply from: [Muxh](https://wordpress.org/support/users/caciocode/)
 * Last activity: [12 years, 7 months ago](https://wordpress.org/support/topic/using-wordpress-rewrite-rules/#post-4284193)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
