/* 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); } }
我发现这有两个问题.
>如果将其设置为默认永久链接,则不会添加规则.
>更重要的是,除非用户更改固定链接设置,否则不会添加规则. (可以通过在插件激活时执行的$wp_rewrite-> flush_rules()来解决)
IIS (common on Windows servers) does not support mod_rewrite.
来源:http://codex.wordpress.org/Using_Permalinks#Permalinks_without_mod_rewrite
听起来并非所有系统都使用.htaccess.因此,直接编辑.htaccess文件可能不是分布式插件的最佳选择.我不知道.可能我必须检查服务器是否使用Apache,如果需要,我需要检查.htacess是否可写并且现有规则没有添加规则,最后我可以将规则附加到它.此外,当用户停用插件时,必须删除规则.所以这很麻烦.
如果wordpress可以使用内置的API或其他东西来处理它,我想把它留给wordpress.但上面的例子是我迄今为止所能找到的.所以我感谢您的信息.
更新
正如pfefferle建议的那样,我可以使用$wp_rewrite-> flush_rules().然而,问题#1仍然存在;使用默认永久链接设置时,它不会产生任何影响.有任何想法吗?
/* Plugin Name: Sample Mod Rewrite */ $custom_mod_rewrite = new custom_mod_rewrite; register_activation_hook( __FILE__,array($custom_mod_rewrite,'flush_rewrite_rules')); register_deactivation_hook( __FILE__,'flush_rewrite_rules')); add_action('generate_rewrite_rules',"generate_rewrite_rules")); class custom_mod_rewrite { function __construct() { $this->wp_rewrite = & $GLOBALS["wp_rewrite"]; } function flush_rewrite_rules() { $this->wp_rewrite->flush_rules(); } function generate_rewrite_rules() { $non_wp_rules = array( 'simple-redirect/?$plugin_name' => 'http://google.com',$rules); } }
此外,停用插件时,它不会更改回先前的规则.我只是遵循了codex示例,只是将其设置为在停用插件时刷新规则.所以应该有一些代码来删除添加的规则.
作为旁注,根据the codex,
Flushing the rewrite rules is an expensive operation,… … you should flush rewrite rules on the activation hook of a plugin,or when you know that the rewrite rules need to be changed
剩余问题: