.htaccess 当永久链接结构被禁用时,如何使用添加\重写\规则函数?

.htaccess 当永久链接结构被禁用时,如何使用添加\重写\规则函数?,.htaccess,url-rewriting,wordpress,permalinks,.htaccess,Url Rewriting,Wordpress,Permalinks,我正在使用add_rewrite_rule()函数修改我的URL结构 我想使用add_rewrite_rule来添加自定义规则,但只有在我的永久链接设置区域中选择了默认设置以外的设置时,才能添加这些规则 i、 e.在设置中有以下选项: - Default http://localhost/wordpress/?p=123 - Day and name http://localhost/wordpress/2014/08/14/sample-post/

我正在使用add_rewrite_rule()函数修改我的URL结构

我想使用add_rewrite_rule来添加自定义规则,但只有在我的永久链接设置区域中选择了默认设置以外的设置时,才能添加这些规则

i、 e.在设置中有以下选项:

 - Default             http://localhost/wordpress/?p=123

 - Day and name        http://localhost/wordpress/2014/08/14/sample-post/

 - Month and name      http://localhost/wordpress/2014/08/sample-post/

 - Numeric             http://localhost/wordpress/archives/123

 - Post name           http://localhost/wordpress/sample-post/

 - Custom Structure    http://localhost/wordpress

因此,当我选择“其他”然后选择“默认”时,我的add_rewrite_rule()函数可以工作,但当选择“默认”时,该函数似乎无法工作。因此,请建议我如何在任何条件下工作的功能。任何帮助都将受到感谢

更新: 我认为问题在于: 使用此选项时,在选择“默认值”时:

get_option('permalink_structure');
我什么都没有

而在其他情况下,有一些值,如:

/%postname%/

/archives/%post_id%

/%year%/%monthnum%/%postname%/

默认的permalinks或所谓的“丑陋”permalinks没有向.htaccess文件添加任何内容,因此Apache重写引擎没有启用。没有重写引擎,就无法进行重写。因此,简单的回答是,使用默认永久链接不可能进行重写

我建议您将重写与查询变量一起使用。添加重写规则时,将自定义数据传递给查询变量,并围绕该查询变量构建功能。这样,您的功能将在所有情况下以及所有permalink类型下都能工作

例如,如果您有以下规则:

add_rewrite_rule('^sometest/([^/]*)/?','index.php?custom_query_var=$matches[1]', 'top');
您可以使用以下代码将
custom\u query\u var
添加为查询变量:

function add_query_vars_filter( $vars ){
    $vars[] = "custom_query_var";
    return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );
然后,当选择默认永久链接时,以下URL将适用于您:

如果选择了“漂亮的”永久链接,URL重写将起作用,您的URL将如下所示:


这基本上是重写所能达到的最佳效果。

我同意@Martin的观点。这里有一个资源可以帮助你

哦,谢谢马林的精彩描述,这意味着我的问题没有解决方案。i、 e.在没有重写引擎的情况下使用add_rewrite_rules()?不,不幸的是没有解决方案。重写是基于重写引擎的,因此引擎必须打开才能使重写工作。就我个人而言,我不会把这归类为一个问题——这与其说是一个问题,不如说是一个限制。谢谢你,阿纳帕姆,这是一个非常好的链接。
use this:
function my_add_query_vars( $qvars ) {


        $qvars[] = 'business-coaching';
        $qvars[] = 'country';
        $qvars[] = 'territory'; 
        $qvars[] = 'region';        

    return $qvars;

}
add_action('query_vars', 'my_add_query_vars');
//Write the rule 
function add_analytic_rewrite_rule()
{
    // Regex:The regex to match the incoming URL is:business-coaching(/([^/]+))?(/([^/]+))?(/([^/]+))?/?
    // Redirect Rule :The resulting internal URL: `index.php` because we still use WordPress
    // `pagename` or page_id=45 because we use this WordPress page
    // `country` : we will assign the first captured regex part to this variable
    // `territory` we will assign the second captured regex part to this variable
    // `region` we will assign the third captured regex part to this variable
    add_rewrite_rule('business-coaching(/([^/]+))?(/([^/]+))?(/([^/]+))?/?','index.php?page_id=45&country=$matches[2]&territory=$matches[`enter code `enter code here`here`4]&region=$matches[6]','top');//superfinal

}
add_action('init', 'add_analytic_rewrite_rule');