Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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 '/'
    $wp_rewrite->add_external_rule( 'images/thumb/(.*)$', 'timthumb.php?filename=$1' );
}
share|improve this answer
    
Thank you so much! I got it worked. I just flush the rules on activation theme. –  wow Feb 21 '11 at 16:41
    
add_action('admin_init', 'wpse9966_init'); call on theme activation and I add $wp_rewrite->flush_rules(true); in the function and it work like a charm. If without $wp_rewrite->flush_rules(true); this won't work. –  wow Feb 21 '11 at 16:52
    
@wow: You should not call flush_rules() on every init, only when the rules change (when the user changes the setting). This is an "expensive" operation, it takes some time. –  Jan Fabry Feb 21 '11 at 20:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.