简体   繁体   中英

Adding mod_rewrite Rules to .htaccess in WordPress

I'm trying to modify the mod rewrite rule in WordPress without directly modifying the .htaccess file. It seems to be possible according to this web page: Adding mod_rewrite Rules to .htaccess in WordPress . I followed the instruction and created a plugin as follows. I saved the file name as sample-mod-rewrite.php .

/* Plugin Name: Sample Mod Rewrite  */

add_action('generate_rewrite_rules', array(&$this, 'generate_rewrite_rules'));
add_filter('mod_rewrite_rules', array(&$this, 'mod_rewrite_rules'));
function generate_rewrite_rules() {
    global $wp_rewrite;
    $non_wp_rules = array(
        'simple-redirect/?$plugin_name' => 'http://google.com',
        'one-more-redirect/?$plugin_name' => 'http://yahoo.com'
    );

    $wp_rewrite->non_wp_rules = $non_wp_rules + $wp_rewrite->non_wp_rules;
}
function mod_rewrite_rules($rules) {
    $rules = preg_replace('/^(RewriteRule ^.*+/?$)plugin_name (/)(.*) ([QSA,L])$/im', '1 3 [R=301,L]', $rules);
    return $rules;
}

How is it supposed to work? After activating the plugin, when I access http://siteurl/simple-redirect/?sample-mod-rewrite or http://siteurl/simple-redirect/?sample-mod-rewrite.php the browser just displays the 404 error, "Object not found!." I think I must be doing something wrong.

The part array(&$this, 'generate_rewrite_rules') looks like passing a class method but the web page does not tell anything about creating a class. It should be really simple.

Thanks.

Since your plugin do not use a class, you should use :

add_action('generate_rewrite_rules', 'generate_rewrite_rules');
add_filter('mod_rewrite_rules', 'mod_rewrite_rules');

The regexp used in mod_rewrite_rules is not really correct, but I can't give you a better one since I don't know what you really want to achieve.

And don't forget to regenerate your .htaccess file.

EDIT :

To regenerate your .htaccess file, you simply have to access your permalinks settings.

The function generate_rewrite_rules will add the following rules in .htaccess :

RewriteRule ^simple-redirect/?$plugin_name /wordpress/http://google.com [QSA,L]
RewriteRule ^one-more-redirect/?$plugin_name /wordpress/http://yahoo.com [QSA,L]

And the goal of mod_rewrite_rules function is to correct these rules, you can try :

$rules = preg_replace('#^(RewriteRule \^.*/\?\$)plugin_name .*(http://.*) \[QSA,L\]#mi', '$1 $2 [R=301,L]', $rules);

Then the rules will be :

RewriteRule ^simple-redirect/?$ http://google.com [R=301,L]
RewriteRule ^one-more-redirect/?$ http://yahoo.com [R=301,L]

So accessing siteurl/simple-redirect/ will redirect to google.com ...

I'm posting the working complete code. I wrapped it in a class.

/* Plugin Name: Sample Mod Rewrite  */

add_action('generate_rewrite_rules', array(new custom_mod_rewrite, "generate_rewrite_rules"));

class custom_mod_rewrite {
    function __construct() {
        $this->wp_rewrite = & $GLOBALS["wp_rewrite"];
    }
    function generate_rewrite_rules() {

        $non_wp_rules = array(
            'simple-redirect/?$plugin_name' => 'http://google.com',
            'one-more-redirect/?$plugin_name' => 'http://yahoo.com'
        );

        $this->wp_rewrite->non_wp_rules = $non_wp_rules + $this->wp_rewrite->non_wp_rules;
        add_filter('mod_rewrite_rules', array(&$this, "mod_rewrite_rules"));
    }
    function mod_rewrite_rules($rules) {
        return preg_replace('#^(RewriteRule \^.*/)\?\$plugin_name .*(http://.*) \[QSA,L\]#mi', '$1 $2 [R=301,L]', $rules);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM