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.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 - 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.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 文件)。

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.
相關文章
相關標籤/搜索