将数字附加到url不会中断链接。

时间:2012-10-30 作者:Ivan Novak

我正在做一些url和元清理,发现了一个奇怪的问题(?)其中,向url添加数字时,url仍然有效。例如:

以下URI可以工作并检索相同的信息:

/2012/06/21/graduation

/2012/06/21/graduation/29/

此数字添加适用于网站上的任何url。

我想知道几件事:

这正常吗

1 个回复
最合适的回答,由SO网友:fuxia 整理而成

这是一个老问题,我从来没有深入研究过,也没有解释过。But 我有一个插件,written a long time ago. 它仍然有效,但在阅读时要记住代码的年代。:)我没有为这篇报道做太多改变。

<?php
/*
Plugin Name: T5 Canonical Permalink
Plugin URI: http://toscho.de/2010/wordpress-plugin-canonical-permalink/
Description: Removes illegal numeric suffixes from the request URI.
Version: 0.3
Author: Thomas Scholz
Author URI: http://toscho.de
Created: 04.04.2010
*/

add_action(\'wp\', \'t5_canonical_request\');
/**
 * WordPress allows URIs with any numeric suffix, e.g.:
 * /canonical-page-or-postname/12345/
 * This functions performs a simple check and redirects
 * to the canonical URI if neccessary.
 *
 * @return void
 */
function t5_canonical_request()
{
    global $page, $post;

    // post, page, attachment, preview
    if ( ! is_singular() or is_preview() )
    {
        return;
    }

    $permalink = get_permalink();

    // We don\'t have access to the number of sub pages here.
    // So we have to hack.
    $max_pages = substr_count(
        $post->post_content, \'<!--nextpage-->\') + 1;

    if ( 1 < $page and $page <= $max_pages )
    {
        /*
         * Handle different permalink settings, eg:
         * /%year%/%postname%.html or
         * /%year%/%postname%/
         */
        $rev_perma_struct = strrev(get_option(\'permalink_structure\'));

        if ( \'/\' != $rev_perma_struct[0] )
        {
            $permalink .= "/$page";
        }
        else
        {
            $permalink .= "$page/";
        }
    }

    $host_uri       = \'http\'
                    . ( empty ( $_SERVER[\'HTTPS\'] ) ? \'\' : \'s\' )
                    . \'://\' . $_SERVER[\'HTTP_HOST\'];
    $canonical_path = str_replace($host_uri, \'\', $permalink);

    if ( ! empty ( $_GET ) )
    {
        global $wp;
        // Array
        $allowed = $wp->public_query_vars;

        $out_arr = array();

        foreach ( $_GET as $k => $v )
        {
            if ( in_array($k, $allowed ) )
            {
                $out_arr[] = $k . ( empty ( $v ) ? \'\' : "=$v" );
            }
        }

        if ( ! empty ( $out_arr ) )
        {
            $canonical_path .= \'?\' . implode(\'&\', $out_arr);
        }
    }

    if ( $canonical_path == $_SERVER[\'REQUEST_URI\'] )
    {
        return;
    }
    // Debug current result:
    #print \'<pre>\' . var_export($canonical_path, TRUE) . \'</pre>\';

    // Change it or return \'false\' to stop the redirect.
    $canonical_path = apply_filters(
        \'t5_canonical_path\',
        $canonical_path
    );

    if ( FALSE != $canonical_path )
    {
        header(\'Location: \' . $permalink, true, 301);
        die("<a href=\'$permalink\'>$permalink</a>");
    }

    return;
}

结束

相关推荐

URLs of plugin resources?

我正在尝试将插件的脚本和css文件添加到管理标头中。是否有类似的功能get_bloginfo(\'url\') 我可以使用它来正确引用文件,而不必硬编码url?