url rewriting - Wordpress rewrite rules don't need ^?

For example this codeclass My_Sitemap{public static function install(){add_filter('rewrite_rules_array', arr

For example this code

class My_Sitemap
{
    public static function install()
    {
        add_filter('rewrite_rules_array', array(__CLASS__, 'rewriteRules'), 1, 1);
        global $wp_rewrite;
        $wp_rewrite->flush_rules(false);
    }

    public static function rewriteRules($wpRules)
    {
        $rules = array(
            'sitemap\.xml$' => 'index.php?pagename=my-sitemap',
        );
        return array_merge($rules, $wpRules);
    }

    public static function parseRequest($wp)
    {
        if (!isset($wp->query_vars['pagename'])) {
            return true;
        }
        if ($wp->query_vars['pagename'] !== 'minimal-xml-sitemap') {
            return true;
        }
        self::outputXML();
    }

    public static function outputXML()
    {
        echo 'hello';
    }
}

register_activation_hook(__FILE__, array('My_Sitemap', 'install'));
add_action('parse_request', array('My_Sitemap', 'parseRequest'));

the rewrite rule is sitemap\.xml$, shouldn't it be ^sitemap\.xml$? If I go to mysite/asitemap.xml it doesn't work, but mysite/sitemap.xml works. Which one is correct?

For example this code

class My_Sitemap
{
    public static function install()
    {
        add_filter('rewrite_rules_array', array(__CLASS__, 'rewriteRules'), 1, 1);
        global $wp_rewrite;
        $wp_rewrite->flush_rules(false);
    }

    public static function rewriteRules($wpRules)
    {
        $rules = array(
            'sitemap\.xml$' => 'index.php?pagename=my-sitemap',
        );
        return array_merge($rules, $wpRules);
    }

    public static function parseRequest($wp)
    {
        if (!isset($wp->query_vars['pagename'])) {
            return true;
        }
        if ($wp->query_vars['pagename'] !== 'minimal-xml-sitemap') {
            return true;
        }
        self::outputXML();
    }

    public static function outputXML()
    {
        echo 'hello';
    }
}

register_activation_hook(__FILE__, array('My_Sitemap', 'install'));
add_action('parse_request', array('My_Sitemap', 'parseRequest'));

the rewrite rule is sitemap\.xml$, shouldn't it be ^sitemap\.xml$? If I go to mysite/asitemap.xml it doesn't work, but mysite/sitemap.xml works. Which one is correct?

Share Improve this question asked Jul 5, 2020 at 21:45 cooldude101cooldude101 1417 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

I just did a test and it seems like the rewrite rules imply ^ if you do not specify it. In my test I found:

rule 'foo' matched url 'foo'
rule '^foo' matched url 'foo'
rule 'foo' did not match url 'afoo'
rule '^foo' did not much url 'afoo'
rule '.*foo' matched url 'afoo'

So i had to explicitly add some matching regex at the start for it to match extra characters at the start. 'foo' did not match 'afoo'

Personally, I like to be specific, so I think you're right it makes sense to make your regex '^sitemap\.xml$' because that's what it will do.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742290347a4416072.html

相关推荐

  • url rewriting - Wordpress rewrite rules don't need ^?

    For example this codeclass My_Sitemap{public static function install(){add_filter('rewrite_rules_array', arr

    1天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信