IIS 7 Rewrite with parent categories

I have seen quite a few questions on here asking around the same thing but either have been unanswered or slightly different!

I have tested the following on a linux machine and all works fine, when I transfer it to windows to run under iis7 it breaks. So I know its setup right as far as the post type and taxonomy code goes.

Read More

I create a product(my custom post type) and have the following as product categories(custom taxonomy):
– test1
— test2
— test3
—- test 4

I assign the product to the test 4 category. So on my linux machine http://localhost/products/test1/test2/test3/test4/product-1/ works perfect. On IIS7 it breaks BUT if I assign the product to the test 1 category it works so:
http://localhost/products/test1/product-1/

I am using the ‘Custom Post Type Permalinks’ plugin and have my posttype permalink set to:
‘/%product_categories%/%postname%/’

Here is my web.config file:

<configuration>
  <system.webServer>
    <rewrite>
    <rules>
        <rule name="Main Rule" stopProcessing="true">
            <match url=".*" />
            <conditions logicalGrouping="MatchAll">
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            </conditions>
            <action type="Rewrite" url="index.php" />
        </rule>
    </rules>
   </rewrite>
  </system.webServer>
</configuration>

Any ideas would be great!

— Edit —
I also just tried this http://localhost/products/test4/product-1/ and it worked. So its just not liking the parent categories.

Related posts

Leave a Reply

1 comment

  1. Finally figured out the answer. It is a mix of multiple different solutions I have found online but here it is!

    First step is to add this to your functions.php

    add_rewrite_rule(
        'products/([^/]+)/([^/]+)/?',       
        'index.php?pmfg_product_categories=$matches[1]&products=$matches[2]',      
        'top'
    );
    

    Secondly you will need this in your web.config

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
    <system.webServer>
    <rewrite>
    <rules><rule name="WordPress Rule" stopProcessing="true"><match url=".*"/><conditions><add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/><add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/></conditions><action type="Rewrite" url="index.php?page_id={R:0}"/></rule>
    <rule name="wordpress" patternSyntax="Wildcard">
    <match url="*"/>
    <conditions>
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
    </conditions>
    <action type="Rewrite" url="index.php"/>
    </rule></rules>
    </rewrite>
    </system.webServer>
    </configuration>

    Lastly here is how I have my custom post type and taxanomy setup. Replace “yourid” with whatever you want to make sure you dont have any issues with other plugins.

    // Register Custom Post Type
    function yourid_product_tax_type(){
        $labels = array(
            'name'                       => _x( 'Product Categories', 'Taxonomy General Name', 'text_domain' ),
            'singular_name'              => _x( 'Product Category', 'Taxonomy Singular Name', 'text_domain' ),
            'menu_name'                  => __( 'Product Categories', 'text_domain' ),
            'all_items'                  => __( 'All Product Categories', 'text_domain' ),
            'parent_item'                => __( 'Parent Category', 'text_domain' ),
            'parent_item_colon'          => __( 'Parent Category:', 'text_domain' ),
            'new_item_name'              => __( 'New Product Category Name', 'text_domain' ),
            'add_new_item'               => __( 'Add New Product Category', 'text_domain' ),
            'edit_item'                  => __( 'Edit Product Category', 'text_domain' ),
            'update_item'                => __( 'Update Product Category', 'text_domain' ),
            'separate_items_with_commas' => __( 'Separate categories with commas', 'text_domain' ),
            'search_items'               => __( 'Search Product Categories', 'text_domain' ),
            'add_or_remove_items'        => __( 'Add or remove product categories', 'text_domain' ),
            'choose_from_most_used'      => __( 'Choose from the most used product categories', 'text_domain' ),
            'not_found'                  => __( 'Product Category Not Found', 'text_domain' ),
        );
        $args = array(
            'label' => 'Product Categories',
            'labels'                     => $labels,
            'hierarchical'               => true,
            'public'                     => true,
            'show_ui'                       => true,
            'show_in_nav_menus'             => true,
            'args'                          => array( 'orderby' => 'term_order' ),
            'rewrite'             => array(
                    'slug' => 'test',
                    'with_front'=>true,                 
                ),
            'query_var'                     => true,
        );
        register_taxonomy( 'yourid_product_categories', array(), $args );
    }
    add_action( 'init', 'yourid_product_tax_type', 0 );
    
    function yourid_post_type() {       
        $labels = array(
            'name'                => _x( 'Products', 'Post Type General Name', 'text_domain' ),
            'singular_name'       => _x( 'Product', 'Post Type Singular Name', 'text_domain' ),
            'menu_name'           => __( 'Products', 'text_domain' ),
            'parent_item_colon'   => __( 'Parent Product: ', 'text_domain' ),
            'all_items'           => __( 'All Products', 'text_domain' ),
            'view_item'           => __( 'View Product', 'text_domain' ),
            'add_new_item'        => __( 'Add New Product', 'text_domain' ),
            'add_new'             => __( 'Add New', 'text_domain' ),
            'edit_item'           => __( 'Edit Product', 'text_domain' ),
            'update_item'         => __( 'Update Product', 'text_domain' ),
            'search_items'        => __( 'Search Product', 'text_domain' ),
            'not_found'           => __( 'Product Not Found', 'text_domain' ),
            'not_found_in_trash'  => __( 'Product not found in Trash', 'text_domain' ),
        );
        $rewrite = array(
            'slug'                => 'products',
            'with_front'          => true,
            'pages'               => true,
            'feeds'               => true,
        );
        $args = array(
            'label'               => __( 'yourid_product', 'text_domain' ),
            'description'         => __( 'YourName Products', 'text_domain' ),
            'labels'              => $labels,           
            'taxonomies'          => array( 'yourid_product_categories' ),
            'hierarchical'        => true,
            'public'              => true,
            'show_ui'             => true,
            'show_in_menu'        => true,          
            'has_archive'         => true,
            'exclude_from_search' => false,
            'publicly_queryable'  => true,
            'rewrite'             => array(
                'slug'=>'products',
                'with_front'=>false,
            ),
            'capability_type'     => 'page',
        );
        register_post_type( 'yourid_product', $args );      
    }
    
    // Hook into the 'init' action
    add_action( 'init', 'yourid_post_type', 0 );
    

    If you have any issues or need assistance let me know!