How to override .htaccess with new rules without ftp or edit it manual

I want script will add new rules to current .htaccess and user no need to ftp or edit it manually.

Example, I use timthumb for resize image on my theme and want rewrite the URL and current .htaccess will be something like this.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^images/thumb/(.*) timthumb.php?filename=$1
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Question

How to add the new rule to the .htacces on my theme option.

Enable Mod rewrite for Timthumb? : [Yes] [No]

If click [Yes] RewriteRule ^images/thumb/(.*) timthumb.php?filename=$1 will automatic added to current .htaccess

Let me know

1 Answer
1

You can add extra rules right after the ^index\.php$ line via the WP_Rewrite class, more specifically the add_external_rule() method. They are added to the non_rewrite_rules array, which is written in the mod_rewrite_rules() method.

This is a very simple example. You should still flush the rewrite rules (once), either on plugin activation or by visiting the Permalinks page.

add_action( 'init', 'wpse9966_init' );
function wpse9966_init()
{
    global $wp_rewrite;
    // The pattern is prefixed with '^'
    // The substitution is prefixed with the "home root", at least a "https://wordpress.stackexchange.com/"
    $wp_rewrite->add_external_rule( 'images/thumb/(.*)$', 'timthumb.php?filename=$1' );
}

Leave a Comment