0

I have a WordPress installation on an IIS7 server. I am currently using permalinks with a setting of /news/%category%/%postname%/ so the URL for all my category landing pages are in the form of /news/{category name}.

I have now set up a category that i do not wish to follow this general pattern. Instead of using the url /news/newcat I would like to use just /newcat.

So far I have been able to set up a rule in my config file as follows:

   <rule name="67" patternSyntax="ExactMatch" stopProcessing="true">
   <match url="newcat/" ignoreCase="true" />
   <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
   <action type="Redirect" url="news/newcat" redirectType="Permanent" />
    </rule>

This does the job but it changes the URL in the address bar - I'm greedy and want everything - redirect the page and show the URL as the 'new' url.

My next thought was to do a rewrite instead and this is where I get stuck. I've tried:

    <rule name="wordpress2" stopProcessing="true">
        <match url="newcat/" />
        <action type="Rewrite" url="index.php?category_name=newcat" />
    </rule>

and a few variations but without success. Can anyone advise please ?

0

I would strongly suggest leaving IIS alone and implement your requirements in WordPress:

/**
 * Remove category base from "newcat" permalink.
 *
 * @param   string  $link
 * @param   object  $term
 * @return  string
 */
function wpse_175424_term_link( $link, $term ) {
    if ( $term->taxonomy === 'category' && $term->slug === 'newcat' )
        $link = home_url( user_trailingslashit( $term->slug ) );
    return $link;
}

add_filter( 'term_link', 'wpse_175424_term_link', 10, 2 );

/**
 * Add our custom rewrite rule to the top of category rules.
 *
 * @param   array   $rules
 * @return  array
 */
function wpse_175424_category_rewrite_rules( $rules ) {
    global $wp_rewrite;

    $matches_1 = $wp_rewrite->preg_index( 1 );
    $new_rules = array(
        'newcat/feed/(feed|rdf|rss|rss2|atom)/?$' => "$wp_rewrite->index?category_name=newcat&feed=$matches_1",
        'newcat/(feed|rdf|rss|rss2|atom)/?$' => "$wp_rewrite->index?category_name=newcat&feed=$matches_1",
        'newcat/page/?([0-9]{1,})/?$' => "$wp_rewrite->index?category_name=newcat&paged=$matches_1",
        'newcat/?$' => "$wp_rewrite->index?category_name=newcat",
    );

    return $new_rules + $rules;
}

add_filter( 'category_rewrite_rules', 'wpse_175424_category_rewrite_rules' );

You'll need to flush your rewrite rules after adding the code (simply visiting the Settings > Permalinks page is sufficient).

Note that I've hardcoded newcat throughout the two functions - if you needed this to be flexible & pulled by ID/option it's easy enough.

| improve this answer | |
  • Thank you for your response however I cannot seem to get this to work. I added the code you provided to the functions.php for the active template and then went to the settings > permalinks page as advised. Is there anything else I need to do ? – KTMcQuaid Jan 21 '15 at 9:20
  • Have you definitely replaced all instances of newcat with the actual category slug? – TheDeadMedic Jan 21 '15 at 12:00
  • Fair question, but yes I definitely have. The error I get is a 403 server error if that helps. – KTMcQuaid Jan 21 '15 at 16:00
  • Problem solved. I actually had a stray folder with exactly the same name as the category I was creating. Simply changing the name of this folder fixed the code. – KTMcQuaid Jan 21 '15 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.