【问题标题】:Wordpress Adding numbers in urlsWordpress 在网址中添加数字
【发布时间】:2014-02-26 10:31:32
【问题描述】:

我的 wordpress 网址有问题。我有下面的网址

http://www.example.com/sample-post/

但在搜索引擎中

http://www.example.com/sample-post/

http://www.example.com/sample-post/500012

http://www.example.com/sample-post/323392

http://www.example.com/sample-post/5

请给我一个解决这个问题的想法。

【问题讨论】:

  • 因为你用的是同一个蛞蝓?或检查 url 模板表单设置
  • 我不明白你的意思我在永久链接中有 /%postname%/

标签: wordpress .htaccess url


【解决方案1】:

我在

上找到的答案

https://wordpress.stackexchange.com/questions/70992/appending-numbers-to-url-do-not-break-the-link

http://toscho.de/2010/wordpress-plugin-canonical-permalink/ 说明:从请求 URI 中删除非法数字后缀。 版本:0.3 作者:Thomas Scholz 作者 URI:http://toscho.de 创建时间:2010 年 4 月 4 日 */

add_action('wp', 't5_canonical_request'); /** * WordPress 允许 URI 带有任何数字后缀,例如:* /canonical-page-or-postname/12345/ * 此函数执行简单的检查并在必要时将 * 重定向到规范 URI。 * * @return 无效 */ 函数 t5_canonical_request() { 全局 $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; }

【讨论】:

    猜你喜欢
    • 2019-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多