Title: Generating Rewrite Rules for Custom Post Type
Last modified: September 1, 2016

---

# Generating Rewrite Rules for Custom Post Type

 *  [damiarita](https://wordpress.org/support/users/damiarita/)
 * (@damiarita)
 * [9 years, 8 months ago](https://wordpress.org/support/topic/generating-rewrite-rules-for-custom-post-type/)
 * Hi,
 * I am developing for a website where I create some Custom Post Types and som Custom
   Taxonomies.
 * I am trying to modify the default WP URLs where the CPT slug is at the beginning.
 * Imagin my CPT is `book`.
 * I want my URLs to change from example.com/book/great-book-title to example.com/
   fantasy/great-book-title (where fantasy is the slug of a term of a custom taxonomy).
 * There are two reasons for this:
    1. I have no archives page, so example.com/book/ is not used
    2. I use Polylang to make it multilingual, but “book” would be in all my URLs
 * What I did:
    1. I filter the CPT URL to match my structure
    2. I add rewrite rules on the top to capture my posts
 * **The issue:**
    I created rewrite rules, but then I realised I was missing example.
   com/fantasy/great-book-title/feed or example.com/fantasy/great-book-title/trackback…
 * I saw what WP was trying to create and I use this function to create the rewrite
   rules:
 *     ```
       function cbm_create_all_rewrite_rules_versions($regex, $query_var, $match_num){
   
           $second_match_num=$match_num + 1;
   
           /*Post itself*/
           add_rewrite_rule('^' . $regex . '/?$','index.php?' . $query_var . '=$matches[' . $match_num . ']', 'top');
           /*Feeds*/
           add_rewrite_rule('^' . $regex . '/feed/(feed|rdf|rss|rss2|atom)/?$','index.php?' . $query_var . '=$matches[' . $match_num . ']&feed=$matches['. $second_match_num . ']', 'top');
           add_rewrite_rule('^' . $regex . '/(feed|rdf|rss|rss2|atom)/?$','index.php?' . $query_var . '=$matches[' . $match_num . ']&feed=$matches[' . $second_match_num . ']', 'top');
           /*Embed*/
           add_rewrite_rule('^' . $regex . '/embed/?$','index.php?' . $query_var . '=$matches[' . $match_num . ']&embed=true', 'top');
           /*Trackback*/
           add_rewrite_rule('^' . $regex . '/trackback/?$','index.php?' . $query_var . '=$matches[' . $match_num . ']&tb=1', 'top');
           /*Paged*/
           add_rewrite_rule('^' . $regex . '/page/?([0-9]{1,})/?$','index.php?' . $query_var . '=$matches[' . $match_num . ']&paged=$matches[' . $second_match_num . ']', 'top');
           /*Comment-page*/
           add_rewrite_rule('^' . $regex . '/comment-page-([0-9]{1,})/?$','index.php?' . $query_var . '=$matches[' . $match_num . ']&cpage=$matches[' . $second_match_num . ']', 'top');
           /*Page*/
           add_rewrite_rule('^' . $regex . '(?:/([0-9]+))?/?$','index.php?' . $query_var . '=$matches[' . $match_num . ']&page=$matches[' . $second_match_num . ']', 'top');
   
       }
       ```
   
 * Is there a way to make WP generate all these possibilities?

The topic ‘Generating Rewrite Rules for Custom Post Type’ is closed to new replies.

## Tags

 * [cpt](https://wordpress.org/support/topic-tag/cpt/)
 * [custom post type](https://wordpress.org/support/topic-tag/custom-post-type/)
 * [custom taxonomy](https://wordpress.org/support/topic-tag/custom-taxonomy/)
 * [rewrite rules](https://wordpress.org/support/topic-tag/rewrite-rules/)
 * [rules](https://wordpress.org/support/topic-tag/rules/)
 * [url](https://wordpress.org/support/topic-tag/url/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 0 replies
 * 1 participant
 * Last reply from: [damiarita](https://wordpress.org/support/users/damiarita/)
 * Last activity: [9 years, 8 months ago](https://wordpress.org/support/topic/generating-rewrite-rules-for-custom-post-type/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
