Support » Fixing WordPress » Custom RewriteRules not working when using WP permalinks

  • Hey guys,

    When I have permalink structure off, my custom RewriteRules in .htaccess work fine. But when I turn it on (to one of the standard by date and postname ones), I can’t seem to get my custom rules to work, even though the WP ones do. I’ve tried turning verbose mode on, no effect, and adding the rules to the function mod_rewrite_rules in classes.php doesn’t work. I’ve also tried adding the rules after the #BEGIN and #END WordPress bits in .htaccess, but still to no effect.

    Nothing is working, and it’s driving me crazy! Does anyone have any ideas that could help me?

    Thanks for your help.

    Regards,

    Weiran.

    P.S. Heres my .htaccess file so you can see what I’ve got:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^kubrick/?$ /index.php?page_id=110 [QSA,L]
    RewriteRule ^k2/?$ /index.php?page_id=126 [QSA,L]
    RewriteRule ^(articles|digressions)/(.*)/?$ /index.php?name=$2 [QSA,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>

    # END WordPress

Viewing 3 replies - 1 through 3 (of 3 total)
  • Using the standard WP 2.0+ .htaccess file, try adding the rewrite rules internally either through a plugin or in functions.php like so:

    function weiran_redirect($rules) {
    $newrules['^kubrick/?$'] = '/index.php?page_id=110';
    $newrules['^k2/?$'] = '/index.php?page_id=126';
    $newrules['^(articles|digressions)/(.*)/?$'] = '/index.php?name=$matches[2]';
    $rules = array_merge($newrules,$rules);
    return $rules;
    }
    add_filter('rewrite_rules_array', 'weiran_redirect');

    Note: WP 2.02 caches the internal rewrite rules, so you will probably have to resave your permalinks under Options > Permalinks before this takes effect.

    Thanks filosofo, it worked perfectly! Any tips on how to put this into a plugin? I’ve never written one before!

    Plugins are easy to make. You just have PHP with a header at the top. Copy the header from another plugin (say the hello.php that comes with your installation) and change the info in the header.

    You can read more about plugins in the Codex.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom RewriteRules not working when using WP permalinks’ is closed to new replies.