• With WP2.0, I wanted to replace my hacked-in mod_rewrite rules with wp_rewrite rules.

    The mod_rewrite rules were as follows:
    RewriteRule ^poetry/([^/]+)/?$ /index.php?name=$1 [QSA,L]
    RewriteRule ^poetry/?$ /index.php?category_name=poetry [QSA,L]

    I replaced them with this in a plugin:
    function cp_rewrite_rules ( $rules ) {
    global $wp_rewrite;

    $new_rules['^poetry/(.+)$'] = '/index.php?name=$matches[1]';
    $new_rules['^poetry/?$'] = '/index.php?category_name=poetry';

    return ( $rules + $new_rules );
    }

    add_filter('rewrite_rules_array', 'cp_rewrite_rules');

    The first rule works properly, showing me a post when I navigate to “/poetry/some-post-name”. The second one should show me the archives for the “poetry” category when I browse to “/poetry/”, but it actually just displays the “home” page.

    Any idea what I’m doing wrong? Thanks.

Viewing 8 replies - 1 through 8 (of 8 total)
  • This rule (no ? after poetry/):

    $new_rules[‘^poetry/$’] = ‘/index.php?category_name=poetry’;

    Should redirect any request for poetry/ to the specified page. Have you tried it without the “?”?

    Thread Starter kccricket

    (@kccricket)

    The ? should specify that the slash is optional. Nevertheless, I have tried without the question mark.

    That seems odd, I know some people have similar strange issues with rewrite rules. When trying to reach the poetry category either way, it gives me a 404 error (checked the server headers) but it still displays the index, yet when I try index.php?category_name=poetry it shows the file not found page in WordPress, but with the proper 404 headers.

    Could it be that the poetry category is empty and thus it has nothing to display? Sorry I can’t be of more help.

    Good luck,
    Michael.

    Thread Starter kccricket

    (@kccricket)

    Just for reference, this is not for use on kccricket.net. The site I’m doing this for is not currently publicly available.

    doing /index.php?category_name=poetry gives me exactly what I expect. The category does exist and does contain at least one post. Thanks, I appreciate the help anyways.

    Thread Starter kccricket

    (@kccricket)

    After much gnashing of teeth and popping of joints, I have discovered my issue.

    WordPress was relying on a stale array of rewrites it had stored in the database (wp_options table). A var_dump($wp_rewrite->wp_rewrite_rules()) revealed that my custom rewrites at the end of the array weren’t changing as I tried different combinations of rules in the plugin.

    Manually dropping the rewrite_rules row in the wp_options table forced WordPress to recreate the ruleset. If I make any other changes to the rules in the plugin, I have to drop that row or the changes don’t take effect.

    Is this a bug, or am I doing something wrong?

    I’m having the same problem. Seems to have just appeared during an upgrade to 2.0.3

    You can fix it by adding this line:

    update_option(‘rewrite_rules’, ”);

    into a an init function:

    add_action(‘init’, ‘clear_htaccess’, 2);

    have you got a working solution yet, that shows how to do this with the filter?

    i’m always getting this error:


    Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'clear_htaccess' was given in /home/brandinfection/www/wp-includes/functions.php on line 1303

    Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/brandinfection/www/wp-includes/functions.php:1303) in /home/brandinfection/www/wp-content/plugins/ncStats/ncStats.php on line 40

    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/brandinfection/www/wp-includes/functions.php:1303) in /home/brandinfection/www/wp-content/plugins/ncStats/ncStats.php on line 40

    Warning: Cannot modify header information - headers already sent by (output started at /home/brandinfection/www/wp-includes/functions.php:1303) in /home/brandinfection/www/wp-content/plugins/ncColors/ncColors.php on line 61

    dax: where do you add the lines starting with ‘update_option’, and ‘add_action’?

    i’ve deleted the rewrite_rules row in wp_options and it still seems to be using old rules.. are the rules stored anywhere else?

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Simple wp_rewrite rule not working’ is closed to new replies.