添加重写规则以指向服务器上的文件

时间:2019-04-17 作者:TheoPlatica

我正在尝试添加一个重写规则,该规则将指向插件中的文件。这可以添加到插件、主题或中。htaccess文件。

主题函数中的当前代码。php看起来像:

// Add Custom Rewrite Rules
function custom_rewrite_basic() {
  flush_rewrite_rules();
  add_rewrite_rule(\'^leaf/([0-9]+)/?\', \'wp-content/plugins/my-plugin/index.php?leaf=$matches[1]\', \'top\');
}
add_action(\'init\', \'custom_rewrite_basic\');
Note: 我正在尝试在基于路径的子域的多站点网络上执行此操作。

2 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

(Revised answer)

  1. Don\'t call flush_rewrite_rules() on init. Instead, if you want to do it programmatically, call the function from your plugin or theme activation function. If you\'re not doing it programmatically, you could simply visit the Permalink Settings page (Settings → Permalinks on the WordPress back-end).

  2. WordPress does support adding a rewrite rule that doesn\'t correspond to index.php, i.e. the second parameter passed to add_rewrite_rule() does not necessarily need to start with index.php — and when it doesn\'t, WordPress will (attempt to automatically) write the rewrite rule to the .htaccess file.

    However, your code should look like so, where the first parameter should not start with a caret (^), and the second parameter should use $1 instead of $matches[1] ($matches[1] is only for rewrite rule which corresponds to the WordPress\'s index.php):

    add_rewrite_rule(\'leaf/([0-9]+)/?\', \'wp-content/plugins/my-plugin/index.php?leaf=$1\', \'top\');
    

    And if your .htaccess file is writable, then after the rewrite rules are flushed, you should see something like this in the file:

    RewriteRule ^index\\.php$ - [L]
    RewriteRule ^leaf/([0-9]+)/? /wp-content/plugins/my-plugin/index.php?leaf=$1 [QSA,L]
    

    The "leaf" value would be available in the (superglobal) $_GET and within your index.php file, you could do:

    <p><?php echo $_GET[\'leaf\']; ?></p>
    
  3. Instead of using add_rewrite_rule(), you could also use the parse_request action hook to handle the URL/request and then load the custom plugin file. Working example:

    <?php
    add_action( \'parse_request\', function( $wp ){
        if ( preg_match( \'#^leaf/([0-9]+)/?#\', $wp->request, $matches ) ) {
            $leaf = $matches[1];
    
            // Load your file - make sure the path is correct.
            include_once plugin_dir_path( __FILE__ ) . \'index.php\';
    
            exit; // and exit
        }
    } );
    

    So as you can see, the "leaf" value is saved to the $leaf variable. So within your index.php file, you could do:

    <p><?php echo $leaf; ?></p>
    

    With the parse_request hook, you don\'t need to worry about your .htaccess file not being writable; however, the $_GET[\'leaf\'] wouldn\'t be available, which explains why we use the $leaf variable. :)

    But yes, although I don\'t recommend it, you could do $_GET[\'leaf\'] = $matches[1]; and the $_GET[\'leaf\'] would be available in your script. ;)

SO网友:TheoPlatica

@Sally CJ,

非常感谢您的回复。你给了我一个很好的指导。我还没有足够的声誉,但我肯定会支持你的回答。

我当前的代码(在子主题中)如下所示:

<?php

    add_action( \'parse_request\', function( $wp ){
      // The regex can be changed depending on your needs
      if ( preg_match( \'#^leaf/(.*)/?#\', $wp->request, $matches ) ) {
        // Get the Leaf Number
        $leaf = $matches[1]; 

        // Define it for my-plugin\'s index.php
        $_GET[\'leaf\'] = urldecode($leaf);

        // Load your file - make sure the path is correct.
        include_once WP_CONTENT_DIR . \'/plugins/my-plugin/index.php\';
        exit; // and exit
      }
    });
您可以在此处测试您的正则表达式:https://www.regextester.com/96691

相关推荐

用于在AWS上使用WordPress MultiSite的映射域的SSL

我已经在Amazon Web服务器EC2实例上安装了Wordpress Multisite。我在实例上使用弹性IP和负载平衡器。我通过AWS证书管理器为多站点设置了通配符SSL证书,该证书适用于主站点和所有新创建的子域网站。我将此证书应用于负载平衡器。现在,我正在尝试为映射域设置SSL证书。我有域名购买通过名称便宜,并已建立了一个记录的弹性IP。我还创建了一个AWS证书,并通过Namecheap DNS上的CNAME记录对其进行了验证。我将此证书应用于同一个负载平衡器,并在AWS中为指向该负载平衡器的子域