Editing and Customizing htaccess Indirectly --间接地编辑和定制htaccess

Editing and Customizing htaccess Indirectly

间接地编辑和定制htaccess


 

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.

如果你想知道如何添加您自己的自定义内容/重写规则到你的WordPress的.htaccess文件,无需手动编辑.htaccess文件,这个信息是给你的。

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:

这是我找到的东西的一个片段:

WordPress .htaccess - Where its creation begins

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.

一个有帮助的知识点是清楚的 .htaccess文件创建/从WordPress的创建。这一切都始于 wp_rewrite【中文】 类,其中位于  wp-includes/rewrite.php 文件

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).

主要感兴趣的类方法 mod_rewrite_rules()(这是负责生成 .htaccess 文件的规则),flush_rules()(调用的是 save_mod_rewrite_rules()),和 save_mod_rewrite_rules()(这写规则到 .htaccess 文件)。

Getting the contents to be written to your .htaccess file

获取内容,然后写入到你的 .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文件的“标记”)。

Adding your own .htaccess contents via WordPress

添加您自己的.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文件的规则…

Add additional non-rewrite .htaccess directives

添加额外的非重写.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, 关于这些规则。

My rules weren't added to the .htaccess file!

我的规则没有添加到.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文件,一旦你添加你的重写钩子,如上所述。

Multisite .htaccess Rules (多站点.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函数,对于多点模式。

Useful Links (有用的链接)

  • WP_Rewrite - The documentation on the WP_Rewrite class is great and very helpful! It's a bit extensive, but very helpful.
  • WP htaccess Control Plugin - The source code in this plugin helped me figure this out. I haven't tried the plugin, but it looks promising.
  • WP_Rewrite Plugin Hooks - This is a list of action/filter hooks you can hook to if you want to alter the WordPress rewrite rules. Very handy.
  • Blog .htaccess Rules - Here's several "most essential .htaccess rules for blogs" that will help with blog performance and might be of interest.
  • .htaccess Rules for Speed - These are some suggested .htaccess rules to help improve your blog/site speed.

 

posted @ 2017-10-31 10:38  kais7mg  阅读(193)  评论(0编辑  收藏  举报