If you are wanting to know how to add your own custom content/rewrite rules to your WordPress .htaccess file without manually editing the .htaccess file, this information is for you.php
若是你想知道如何添加您本身的自定义内容/重写规则到你的WordPress的.htaccess文件,无需手动编辑.htaccess文件,这个信息是给你的。html
Upon recently trying to figure out how to add contents to my WordPress generated .htaccess file, I was surprised at how hard it was to find information on this. Luckily though I came across WP htaccess Control plugin, which showed where and how to hook the necessary action/filter hooks to do what I was trying to do.缓存
在最近试图找出如何添加内容到个人WordPress生成的.htaccess文件,我在找这方面的信息是多么难惊讶。幸运的是,虽然我找到 WP htaccess Control 插件,这代表在哪里和如何挂钩的必要行动/过滤钩子来作我想作的。网络
So here's a run down of what I found:app
这是我找到的东西的一个片断:wordpress
WordPress .htaccess - 是在这个位置开始建立的函数
One helpful bit of knowledge is knowing where the .htaccess
file is created/generated from within WordPress. It all starts with the WP_Rewrite class, which is located in wp-includes/rewrite.php
file.post
一个有帮助的知识点是清楚的 .htaccess文件建立/从WordPress的建立。这一切都始于 wp_rewrite【中文】 类,其中位于 wp-includes/rewrite.php
文件ui
The main class methods of interest are mod_rewrite_rules()
(which is responsible for generating the .htaccess file rules), flush_rules()
(calls save_mod_rewrite_rules()), and the save_mod_rewrite_rules() (which writes the rules to the .htaccess file).this
主要感兴趣的类方法 mod_rewrite_rules()(这是负责生成 .htaccess 文件的规则),flush_rules()(调用的是 save_mod_rewrite_rules()),和 save_mod_rewrite_rules()(这写规则到 .htaccess 文件)。
获取内容,而后写入到你的 .htaccess 文件
If you ever wondered where to grab what is going to be written to the .htaccess file, the mod_rewrite_rules filter hook is the one you're probably interested in.
若是你不知道要抓住什么将要写入.htaccess文件,该 mod_rewrite_rules 过滤器钩子是一个你可能感兴趣的。
This returns a string of the .htaccess generated rules, which allows for appending/pre-pending your own rules and/or modifying the string.
这会返回一个字符串的.htaccess规则,容许 添加/预挂起 本身的规则,而且/或者修改字符串。
Here's an example of using this hook:
下面是使用这个钩子的示例:
<?php /** * Get .htaccess contents before being written to file(获取 .htaccess的内容,在写入文件以前。) * * Uncomment the first two lines and to go Settings > Permalinks to see the output.(不要注释开始的两行,而且到 是设置>固定连接 去查看输出) */ function my_htaccess_contents( $rules ) { // echo '<pre>'. $rules .'# My little addition!\n</pre>'; // exit(); return $rules . "# My little addition!\n"; } add_filter('mod_rewrite_rules', 'my_htaccess_contents');
The output should be something like this:
输出应该是这样的:
RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] # My little addition!
That's an example of how we can append our own content to the end of the generated rewrite rules.
这就是咱们如何将本身的内容附加到生成的重写规则的结尾的一个例子。
Ok, so perhaps you're wondering where the #BEGIN WordPress
and #END WordPress
lines are at....
Well, the answer is in the wp-admin/includes/misc.php
file's save_mod_rewrite_rules() function and it's call to insert_with_markers() (which saves the .htaccess file with the "markers").
好吧,也许你想知道的 #BEGIN WordPress 和 #END WordPress 行在哪…嗯,答案在 wp-admin/includes/misc.php
文件的 save_mod_rewrite_rules()函数和它调用insert_with_markers()(保存到 .htaccess文件的“标记”)。
添加您本身的.htaccess内容经过WordPress
If you are wanting to add .htaccess rules or additional directives when WordPress generates the .htaccess file, here's how to do it.
若是你想添加规则或额外的指令到.htaccess文件,当WordPress生成.htaccess文件的时候,这里是怎么作的。
NOTE:If the rewrite rule is an internal rewrite, it shouldn't be added to the .htaccess file, but instead it should be added to and handled by WordPress's internal rewrites. See the Examples for examples of this.
注意:若是重写规则是一个内部的改写,它不该该被添加到.htaccess文件,而是应该被添加到WordPress的内部重写处理。请参见示例中的示例。
Here's two different ways you might want to add .htaccess file rules...
这是两种不一样的方式,你可能想添加.htaccess文件的规则…
添加额外的非重写.htaccess指令
So say you want to add cache headers based on content, file access restrictions, or whatever else you can find to put in your .htaccess
file.
因此说你想添加基于内容的缓存文件,文件的访问限制,或任何其余你能找到的,写入到.htaccess文件。
Your filter function is passed the generated rules as a string and it MUST be returned.
您的过滤器函数将生成的规则做为字符串传递,必须返回它。
Here you can choose to return it with your rules prepended or appended to the generated rules. Here's an example of that:
在这里你能够选择你的规则 前置添加或后置追加 到生成的规则返回。下面是一个例子:
<?php function my_htaccess_contents( $rules ) { $my_content = <<<EOD \n # BEGIN My Added Content # Protect wpconfig.php <Files wp-config.php> Order Allow,Deny Deny from all </Files> # END My Added Content\n EOD; return $my_content . $rules; } add_filter('mod_rewrite_rules', 'my_htaccess_contents');
This will now make our .htaccess
contents look something like this:
这将使咱们的.htaccess内容看起来像这样:
# BEGIN My Added Content # Protect wpconfig.php <Files wp-config.php> Order Allow,Deny Deny from all </Files> # END My Added Content RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L]
Any rules that don't start with index.php will be added to the .htaccess
files as a normal RewriteRule when the rules are flushed.
任何规则不启动,index.php 将被添加到.htaccess文件,做为一个正常的RewriteRule规则,当规则刷新的时候。
Here's the example of what our .htaccess file will look like with the added rule..
这里的例子,咱们的.htaccess文件看起来像这样,对于添加的规则。
# BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteRule ^myrule /newlocation [QSA,L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule>
GOTCHA: The internal rewrite rules use $matches[1] for referencing matching patterns.
This doesn't work for.htaccess
rules and you must use standard $1 and so forth for those rules.问题:内部重写规则使用 $matches[1] 引用匹配模式。这不适合.htaccess规则,你必须使用标准 $1, 关于这些规则。
个人规则没有添加到.htaccess文件!
Ok, so I haven't told you how to make WordPress re-generate the .htaccess file with the updated rules/directives ...
There's a couple of ways to do this:
好的,因此我没有告诉你如何使WordPress 从新生成 .htaccess文件,更新规则/指令…
有几种方法能够作到这一点:
1. Make a "flush_rules" function. (建立 “flush_rules”函数。)
We can create a function that flushes the rules and hook it to the admin_init
action hook so it'll only be called when any administrative page is loaded.
咱们能够建立一个函数,将规则和钩字到挂钩 admin_init 动做,它将会被调用,在任何管理页面加载的时候。
This was the solution for the WordPress Support topic: Writing $wp_rewrite->non_wp_rules to .htaccess?
The example there is sufficient enough so I won't repeat it.
这是WordPress支持主题的解决方案:Writing $wp_rewrite->non_wp_rules to .htaccess?
2. Visit the Permalink Settings page.(访问固定连接设置页面)
When you visit Permalink Settings and/or change and save the settings, WordPress flushes the rewrite rules and re-generates the .htaccess
file to reflect the changes. So this is probably the easiest way to regenerate the .htaccess
file once you've added your rewrite hooks as mentioned above.
当你访问的永久连接的设置和/或更改而且保存设置,WordPress将重写规则和从新生成.htaccess文件来反映环境的变化。因此,这多是最简单的方法来生成.htaccess文件,一旦你添加你的重写钩子,如上所述。
WordPress Multisite (aka. Network Site) differs from single instances and the fact that the .htaccess rules apply to the entire network, flushing the rules won't re-generate the .htaccess file.
WordPress多站点(又名。网络站点)不一样于单实例 和 事实上.htaccess规则适用于整个网络,刷新规则不会从新生成.htaccess文件。
I am currently unaware of how to add custom .htaccess
content/rules at this time since there doesn't appear to be any built-in WordPress functions for this in Multisite mode.
我如今不知道如何添加自定义.htaccess 内容/规则,在这个时候,从没有出如今任何内置WordPress函数,对于多点模式。