简体   繁体   English

如何阻止.htaccess中的重写规则被Wordpress删除?

[英]How Do I Stop Rewrite Rules in .htaccess from Being Removed by Wordpress?

I am developing a plugin which rewrites a specific URL pattern to a directory within the plugin.我正在开发一个插件,它将特定的 URL 模式重写到插件内的目录中。 To add these rules I call add_rewrite_rule() in the plugin activation function before calling flush_rewrite_rules().为了添加这些规则,我在调用 flush_rewrite_rules() 之前在插件激活 function 中调用了 add_rewrite_rule()。 This does a great job of creating the rewrite rules, but somehow they get removed sometimes.这在创建重写规则方面做得很好,但有时它们会以某种方式被删除。

In one case, I changed the permalink structure for posts.在一种情况下,我更改了帖子的永久链接结构。 Wordpress seemingly regenerated the .htaccess file without my rules. Wordpress 似乎在没有我的规则的情况下重新生成了 .htaccess 文件。 To regenerate the rules I had to deactivate and reactivate the plugin.要重新生成规则,我必须停用并重新激活插件。 This surprised me because I was under the impression that calling flush_rewrite_rules() inventories existing rules before adding new ones and regenerating .htaccess file.这让我感到惊讶,因为我的印象是调用 flush_rewrite_rules() 在添加新规则和重新生成 .htaccess 文件之前清点现有规则。 Something in my system seems to be regenerating the file without inventorying the existing rules first.我系统中的某些东西似乎在没有先清点现有规则的情况下重新生成文件。

In another case, I added some plugins and I think one of them regenerated the .htaccess file when it was activated.在另一种情况下,我添加了一些插件,我认为其中一个在激活时重新生成了 .htaccess 文件。

Is there any way to stop Wordpress and other plugins from removing my rules from the .htaccess file?有没有办法阻止 Wordpress 和其他插件从 .htaccess 文件中删除我的规则?

If there is some way to flag rules so that Wordpress cannot remove them in the future that would be great.如果有某种方法可以标记规则,以便 Wordpress 以后无法删除它们,那就太好了。 Like if you could add a special character or something that tells Wordpress never to remove that specific rule for any reason other than a directive to remove it originating from the plugin which created it.就像你可以添加一个特殊字符或告诉 Wordpress 永远不要出于任何原因删除该特定规则,而不是从创建它的插件中删除它的指令。

What I cannot entertain would be making the .htaccess file unwritable via file permissions because it must be writable for Wordpress to create the rules in the first place.我不能接受的是通过文件权限使 .htaccess 文件不可写,因为它必须是可写的 Wordpress 首先创建规则。 I also believe that I cannot add the rules on init because this is a plugin not a theme.我也相信我不能在 init 上添加规则,因为这是一个插件而不是主题。 This is my first plugin which uses rewrite rules, so I could be wrong about this but according to my research so far you're supposed to add rewrite rules from plugins on activation not init.这是我的第一个使用重写规则的插件,所以我对此可能是错误的,但根据我目前的研究,你应该在激活时而不是初始化时从插件添加重写规则。

Use the 'save_mod_rewrite_rules' filter to modify the .htaccess rules before they are saved.You can use this filter to include your plugin's rewrite rules in the .htaccess file, before the file is saved.使用“save_mod_rewrite_rules”过滤器在保存之前修改 .htaccess 规则。在保存文件之前,您可以使用此过滤器将插件的重写规则包含在 .htaccess 文件中。

 function my_mod_rewrite_rules( $rules ) {
    // Get your plugin's rewrite rules
    $my_rules = my_get_rewrite_rules();

    // Add your plugin's rewrite rules to the beginning of the $rules string
    $rules = $my_rules . $rules;

    return $rules;
}


add_filter( 'mod_rewrite_rules', 'my_mod_rewrite_rules' );

You can use this filter in your plugin's activation function, to add your plugin's rewrite rules to the .htaccess file when your plugin is activated.您可以在插件激活 function 中使用此过滤器,以在插件激活时将插件的重写规则添加到 .htaccess 文件中。

Good luck !祝你好运 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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