Custom RewriteRules not working when using WP permalinks (4 posts)

  1. weiran
    Posted 10 years ago #

    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.



    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]

    # END WordPress

  2. Austin Matzko
    Posted 10 years ago #

    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.

  3. weiran
    Posted 10 years ago #

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

  4. Austin Matzko
    Posted 10 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic