【问题标题】:How to use add_rewrite_rule function, while permalink structure is disabled?如何使用 add_rewrite_rule 功能,而永久链接结构被禁用?
【发布时间】:2014-10-07 16:30:52
【问题描述】:

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

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

即在设置中有以下选项:

 - 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%/

【问题讨论】:

    标签: .htaccess url-rewriting wordpress permalinks


    【解决方案1】:

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

    我可以建议您将重写与查询变量一起使用。添加重写规则时,将自定义数据传递给查询变量,并围绕该查询变量构建功能。这样,您的功能将适用于所有情况和所有永久链接类型。

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

    add_rewrite_rule('^sometest/([^/]*)/?','index.php?custom_query_var=$matches[1]', 'top');
    

    您使用以下代码将custom_query_var 添加为查询变量:

    function add_query_vars_filter( $vars ){
        $vars[] = "custom_query_var";
        return $vars;
    }
    add_filter( 'query_vars', 'add_query_vars_filter' );
    

    然后选择默认永久链接时,以下URL将为您工作:

    http://yoursite.com/index.php?custom_query_var=abc

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

    http://yoursite.com/sometest/abc/

    这基本上是通过重写可以实现的最佳效果。

    【讨论】:

    • 哦,谢谢 Marin 的精彩描述,所以这意味着我的问题没有解决方案。即在没有重写引擎的情况下使用 add_rewrite_rules() ?
    • 不,很遗憾没有解决方案。重写基于重写引擎,因此必须打开引擎才能使重写工作。就个人而言,我不会将此归类为问题 - 它更多的是限制而不是问题。
    【解决方案2】:

    我同意@Martin。这里有一个资源可以帮助https://core.trac.wordpress.org/ticket/15235

    【讨论】:

    • 谢谢 Anupam,这是一个非常好的链接。
    【解决方案3】:
    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');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-15
      • 2021-04-01
      • 2014-06-28
      • 1970-01-01
      相关资源
      最近更新 更多