Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a permalink that leads to some custom content like:

http://mysite.com/extra-deal/91103/

That will take you to a page that is handled by the file:

category-extra-deals.php

On that page I grab the last portion of the URL (91103 in the above example) and use that to do a custom database request for the info I need.

I want to quit using the id number (91103) and start using the title.

However changing that to:

http://mysite.com/extra-deal/title-of-that-entry/

Results in a 404 and the template category-extra-deals.php is never touched.

I figured that the template would be hit, at least, even if that title can't be found but this is not the case.

Another example... if I visit:

http://mysite.com/extra-deal/9/

I will get the category-extra-deals.php template but with no content because 9 doesn't exist.

However using:

http://mysite.com/extra-deal/a/

I will get the 404 page.

Why do I get the desired template when the URL has a number and a 404 when that same portion of the URL has text?


Solution

I needed to add my own rewrite rules to accommodate the new url structure.

So the following url:

http://mysite.com/extra-deal/the-title-of-the-posting

Would be handled by the following rule:

function add_rewrite_rules($aRules) {
$aNewRules = array('extra-deal/([^/]+)/?$' => 'index.php?pagename=extra-deal&extradeal_title=$matches[1]');
$aRules = $aNewRules + $aRules;
return $aRules;
}

add_filter('rewrite_rules_array', 'add_rewrite_rules');

One other thing, if you notice, the url expects a param named "extra deal_title". That needs to be added as well and it done with:

function add_query_vars($aVars) {
$aVars[] = "extradeal_title";
return $aVars;
}
share|improve this question
    
How did you change the URL? Did you change the site's permalinks settings? –  s_ha_dum Aug 8 '13 at 1:27
    
@s_ha_dum No. The permalinks in the admin are: /%category%/%postname%/ Its important to note that the "postname" in this case isn't a real post. This is data that is completely custom and is not a Wordpress post. I hit the template and use my own code to grab the id portion of the url, then I make a SQL request to a table I have created to retrieve the needed data. –  gaoshan88 Aug 8 '13 at 2:09
    
Loading the page must be WordPress. That is, if this is in a WordPress context WordPress must be able to locate http://mysite.com/extra-deal/title-of-that-entry/ or you will get a 404. It doesn't matter what the content is inside the page. I think you need to expand the description. @Krzysiekdrozdz is probably on the right track though. –  s_ha_dum Aug 8 '13 at 14:21
add comment

1 Answer

up vote 1 down vote accepted

WordPress uses set of regular expressions to parse urls (so called rewrite rules). It matches them in some order and stops when first matching rule is found.

It's a little bit hard to say what rule is matched in your case. We don't know what are your permalink settings and what other changes to rewrite rules are made.

To see all these rules, just add this function and then call it (footer.php is a good place to put its call)

function my_debug_rewrite_rules() {
    global $wp_rewrite;
    echo '<div>';
    if ( !empty( $wp_rewrite->rules ) ) {
        echo '<table><thead><tr><th>Rule</th><th>Rewrite</th></tr></thead><tbody>';
        foreach ( $wp_rewrite->rules as $name=>$value ) {
            echo '<tr><td>'. esc_html($name) .'</td><td>'. esc_html($value) .'</td></tr>';
        }
        echo '</tbody></table>';
    }
    echo '</div>';
}

Another thing you can check is which of these rules has been matched:

function debug_page_request() {
    $wp, $template;
    echo '<p>Request: '. esc_html($wp->request) .'</p>';
    echo '<p>Matched Rewrite Rules: '. esc_html($wp->matched_rule) .'</p>';
    echo '<p>Loaded Template: '. esc_html(basename($template)) .'</p>';
}

(Again, call it in footer.php).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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