Rewrite rules in .htaccess get overwritten?

I’m not able to make it work by adding custom rewrite rules into functions.php or adding custom permastructures either. I have this code in my .htaccess that is working fine.

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# BEGIN IPHONE RULES
RewriteRule ^mobile/([^/]+)$ mobile/index.php?action=$1 [QSA,L]
# END IPHONE RULES
# BEGIN PDF RULES
RewriteRule ^certificates/30-Days-Certificate-([0-9]+)$ pdf/index.php?type=30day&period=$1 [QSA,L]
RewriteRule ^certificates/12-Month-Certificate$ pdf/index.php?type=12Month [QSA,L]
# END PDF RULES
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

But every once a while it gets refreshed to defaults and I lose the iPhone and pdf rules. The mobile is called like this /mobile/progress?userId=22&date=2011-02-21 where progress is the action and the others are just query vars. On the .htaccess is working fine but I can’t manage to make it work by adding custom rules. Any help will be much appreciated.

1 Answer
1

The part between the # BEGIN WordPress and # END WordPress will always be rewritten when the permalinks are flushed. You can either place your extra rewrite rules before this segment, or you can add register them in WordPress as external rewrite rules. If you flush your rules now (by visiting the Permalinks page, for example), your extra rules will be added to the .htaccess file.

add_action( 'init', 'wpse12708_init' );
function wpse12708_init()
{
    global $wp_rewrite;
    $wp_rewrite->add_external_rule( 'mobile/([^/]+)$', 'mobile/index.php?action=$1' );
    $wp_rewrite->add_external_rule( 'certificates/30-Days-Certificate-([0-9]+)$', 'pdf/index.php?type=30day&period=$1' );
    $wp_rewrite->add_external_rule( 'certificates/12-Month-Certificate$', 'pdf/index.php?type=12Month' );
}

Leave a Comment