多个驻留域名的根相对链接

时间:2012-04-28 作者:Gregory

我有五个指向同一服务器/wp安装的域。域名很重要,我的访问者在浏览我的网站时需要能够停留在他们喜欢的域名上。

如何设置wp(或.htaccess或…)这样所有链接都指向访问者指定的域/主机?

现在,如果我将Dashboard/General/Site Address设置为Site-A.com,并将站点加载为Site-b.com,则链接仍然指向Site-A.com,而不是Site-b.com;例如,负载http://site-B.com/blog/this-is-my-home/. 加载页面中的链接仍然指向site-A.com,而不是site-b.com。

在我的旧MT系统中,我能够将所有链接定义为根相对的,因此所有链接都指向当前域。可湿性粉剂是否可以?

干杯,格雷戈里

2 个回复
SO网友:Gregory

我跟踪了我的主题(Chip Bennett的Oenology子主题)和生成链接的wp include中的函数调用,发现home_url() 函数,然后为我的站点编写以下函数:

function gregory_make_relative($url=\'\') {
    return preg_replace( \'#^https?://[^/]+/#iu\', \'/\', $url, 1 );
}
add_filter( \'home_url\', \'gregory_make_relative\', 11, 1 );
我故意在域后搜索grep以包含/以便我仍然可以使用home_url(\'\') 返回博客的指定域,并在<head> 通过WordPress SEO使用以下功能(即,无论用于加载页面的域是什么,规范链接都是相同的;即,没有“重复内容”):

function gregory_wpseo_canonical_add_domain( $canonical ) {
    return home_url(\'\').$canonical;
}
add_filter( \'wpseo_canonical\', \'gregory_wpseo_canonical_add_domain\', 10, 1 );
到目前为止,它运行得非常好,但我想知道它是否会对feed或我最终实现的电子商务解决方案产生负面影响。欢迎评论、注释、提示和警告:-)

更新一个简单的home_url() (即,未指定路径)在整个系统中使用,因此无法使用grep搜索中的尾随/。我不得不删除它,并找到另一种方法来指定规范url中的域。因此,通过wp进行的更多研究包括,现在函数如下所示:

function gregory_make_relative($url=\'\') {
    return preg_replace( \'#^https?://[^/]+#iu\', \'\', $url, 1 );
}
add_filter( \'home_url\', \'gregory_make_relative\', 11, 1 );

function gregory_wpseo_canonical_add_domain( $canonical ) {
    // get_option() is defined in wp-includes/functions.php and is used by get_home_url()
    return get_option(\'home\').$canonical;
}
add_filter( \'wpseo_canonical\', \'gregory_wpseo_canonical_add_domain\', 10, 1 );

更新2

比最初出现的更难;-)我的代码现在看起来像这样。馈送受到影响在生成链接的函数链中的某个位置,提要似乎正在使用\\u服务器[\'HTTP\\U主机\']我必须在那里检查我的选择。

/* FILTERS TO PRODUCE ROOT-RELATIVE URLs */
// define WP_SITEURL because the formula in wp-includes/functions::wp_guess_url() makes a
// false assumption and appends $_SERVER[\'REQUEST_URI\'] to the base_url.
define(\'WP_SITEURL\', \'http://my.domain.hk/\', true );

// strip the domain
function gregory_make_relative( $url=\'\' ) {
    return preg_replace( \'#^https?://[^/]+#iu\', \'\', $url, 1 );
}
add_filter( \'site_url\', \'gregory_make_relative\', 11, 1 );
add_filter( \'home_url\', \'gregory_make_relative\', 11, 1 );
add_filter( \'template_directory_uri\', \'gregory_make_relative\', 11, 1 );
add_filter( \'stylesheet_directory_uri\', \'gregory_make_relative\', 11, 1 );
add_filter( \'script_loader_src\', \'gregory_make_relative\', 11, 1 );

function gregory_make_stylehref_relative( $tag=\'\' ) {
    // $wp_styles->do_item() passes this along to the filter:
    // "<link rel=\'$rel\' id=\'$handle-rtl-css\' $title href=\'$rtl_href\' type=\'text/css\' media=\'$media\' />\\n"
    $matches = array();
    if( !preg_match( \'#^(.+ +href=\\\')(.+)(\\\' +type=.+)$#iu\', $tag, &$matches ))
        return $tag;
    $matches[2] = gregory_make_relative($matches[2]);
    return $matches[1].$matches[2].$matches[3];
}
add_filter( \'style_loader_tag\', \'gregory_make_stylehref_relative\', 11, 1 );

function gregory_wpseo_canonical_add_domain( $canonical=\'\' ) {
    // get_option is defined in wp-includes/functions.php and is used by get_home_url() to get the home url.
    return get_option(\'home\').$canonical;
}
add_filter( \'wpseo_canonical\', \'gregory_wpseo_canonical_add_domain\', 10, 1 );

更新3——WPSEO中有一个选项,可以在RSS帖子前后添加文本/链接,包括使用占位符的选项。其中一个占位符是%%BLOGLINK%%. 不幸的是,在根相对筛选器就绪的情况下,%%BLOGLINK%%生成了一个空字符串,该字符串在提要中没有用处。这段代码解决了这个问题(注意,我的另一个选择是简单地在WPSEO的RSS设置中硬编码链接,这可能是更明智的做法:-)

// a change for WPSEO\'s %%BLOGLINK%% code.
// I changed get_bloginfo() to get_bloginfo_rss() in wpseo/frontend/class-frontend.php to allow this.
// without these changes, %%BLOGLINK%% is printed into the rss feeds as an empty string.
function gregory_set_domain_in_rss_urls( $info, $show ) {
    // copied from wp-includes/general-template.php::get_bloginfo()
    $url = true;
    if (strpos($show, \'url\') === false &&
        strpos($show, \'directory\') === false &&
        strpos($show, \'home\') === false)
        $url = false;

    return ( !$url || !empty($info) ? $info : \'/\' );
}
add_filter( \'get_bloginfo_rss\', \'gregory_set_domain_in_rss_urls\', 11, 2 );
(我决定在发布消息后对RSS进行硬编码,因此在我的主题中禁用了上述过滤器。)


更新4--preg\\u replace()变为preg\\u match()

我已经更新了gregory_make_stylehref_relative() 更新2中显示的要使用的函数preg_match() 而不是preg_replace(). 这是旧代码:

    $href = preg_replace( \'#^(.+ +href=\\\')(.+)(\\\' +type=.+)$#iu\', \'$2\', $tag );
    $href = gregory_make_relative($href);
    return preg_replace( \'#^(.+ +href=\\\')(.+)(\\\' +type=.+)$#iu\', \'$1\'.$href.\'$3\', $tag );

更新5——get\\u avatar()

另一个过滤器,这次是get\\u avatar(),以便模式和当前域包含在wp的空白路径中。gif。如果没有这些,将无法找到和加载gif。如果没有该模式,Gravatar将加载自己的公司头像。

function gregory_avatar_add_domain( $avatar ) {
    // look for urlencode( includes_url(\'images/blank.gif\')) in the $avatar string.
    // if found, encode the schema and domain, insert it into the $avatar string.
    $gif = includes_url(\'images/blank.gif\'); // from get_avatar()
    if( preg_match( \'|^https?://|i\', $gif ))
        // the url already includes the schema (and domain).
        return $avatar;
    $gif = urlencode($gif);
    $schema = is_ssl() ? \'https://\' : \'http://\'; // from wp_guess_url()
    $domain = urlencode( $schema . $_SERVER[\'HTTP_HOST\'] );
    return str_replace( $gif, $domain.$gif, $avatar );
}
add_filter( \'get_avatar\', \'gregory_avatar_add_domain\', 11, 1 );

更新6—redirect\\u canonical()

筛选器导致涉及查询字符串的传入url出现问题。url的scheme://domain 会被砍成://domain. 花了几个小时才找到问题,但它在wp includes/canonical中。redirect\\u canonical()和那里的一个过滤器挂钩可以纠正这个问题。以下是过滤器:

function gregory_redirect_canonical_addScheme( $redirect_url ) {
    // redirect_canonical() requires (but doesn\'t need) a fully qualified url.
    // get_permalink() in the second iteration of redirect_canonical()
    // (redirect_canonical() calls itself) usually switches to the domain specified in the
    // WP Settings. but if we do the same, redirect_canonical() doesn\'t recognise the
    // permalink as a redirect, and WP doesn\'t update the url in the User Agent\'s Address bar.
    if( preg_match( \'|^https?://|i\', $redirect_url ))
        // fully qualified url. leave it alone.
        return $redirect_url;
    if( substr( $redirect_url, 0, 3 ) == \'://\' )
        // no scheme specified in $redirect during the
        // second pass through redirect_canonical().
        return (is_ssl() ? \'https\' : \'http\') . $redirect_url;
    if( substr( $redirect_url, 0, 1 ) == \'/\' )
        // root-relative url.
        return (is_ssl() ? \'https://\' : \'http://\').$_SERVER[\'HTTP_HOST\'].$redirect_url;
    // relative url. not root-relative.
    return (is_ssl() ? \'https://\' : \'http://\').$_SERVER[\'HTTP_HOST\'].\'/\'.$redirect_url;
}
add_filter( \'redirect_canonical\', \'gregory_redirect_canonical_addScheme\', 11, 1 );

请注意,我还没有将我的问题标记为刚才的答案,因为我不知道这些过滤器在今后会产生什么后果。需要更多的时间和测试。

干杯,格里高利(WordPress 3.3.2)

SO网友:Gregory

恢复到绝对url几乎是可行的,但很复杂,因为WP是使用绝对url编写的,并且需要绝对url。我意识到,我只想让访问我网站的人能够看到使用与其指定域相同的域的所有链接。这并不要求使用根相对url。所以我重写了代码,用访问者指定的域替换所有相关url中的域。这个complete codemuch simpler!

// set the url\'s domain to that of the current host, IGNORING any specified subdomains.
// (other WordPress users may need to retain the subdomains)
function gregory_use_host_domain( $url=\'\' ) {

    $myDomains = \'(?:domain1|domain2|domain3|xn--domain4|xn--domain5)\';
    $matches = array();

    // get the current host domain. ignore/remove any subdomain.
    if( 1 != preg_match( \'!.*(\'.$myDomains.\'\\.hk)!iu\', $_SERVER[\'HTTP_HOST\'], &$matches )) // preg_match returns 1 if the pattern was found.
        // the current host is not one of the above specified domains! weird. this will probably never happen, but just in case...
        return $url;
    $thisDomain = ( is_ssl() ? \'https://\' : \'http://\' ).$matches[1];

    if( 1 == preg_match( \'!^(?:https?://[^/]*\'.$myDomains.\'\\.hk)(/.*)?$!iu\', $url, &$matches ))
        // fully qualified url.
        return $thisDomain.$matches[1];
    if( substr( $url, 0, 1 ) == \'/\' )
        // root-relative url.
        return $thisDomain.$url;
    // relative url but not root-relative.
    return $thisDomain.\'/\'.$url;
        
}
add_filter( \'site_url\', \'gregory_use_host_domain\', 11, 1 );
add_filter( \'home_url\', \'gregory_use_host_domain\', 11, 1 );

/*
 * CONTENT_URL()
 * this function directly affects the template_ and stylesheet_ directory uri results,
 * but they don\'t matter as far as the visitor is concerned. they\'re not going to directly
 * access those particular url\'s.
 *
 * SPAM-FREE-WORDPRESS PLUGIN
 * the Spam-Free-Wordpress plugin uses plugin_dir_url() and plugin_dir_path() to get the paths to its resources,
 * but we\'re not going to worry about specifying the domain for these items.
 * wp\'s plugin_dir_url() uses wp\'s plugins_url()
 * wp\'s plugin_dir_path() uses php\'s dirname()
 */


// a filter to specify the \'domain1.hk\' domain for WPSEO\'s canonical links.
// one specific domain for all content canonical links. no \'duplicate\' content.
function gregory_wpseo_specify_canonical_domain( $canonical=\'\' ) {

    $matches = array();
    if( 1 != preg_match( \'!^(?:https?://[^/]+)(/.*)?$!iu\', $canonical, &$matches ))
        return $canonical;
    // get_option is defined in wp-includes/functions.php and is used by get_home_url() to get the home url.
    return get_option(\'home\').$matches[1];
}
add_filter( \'wpseo_canonical\', \'gregory_wpseo_specify_canonical_domain\', 10, 1 );
干杯,格雷戈里(Gregory)(WordPress 3.4.2)

结束

相关推荐

Archive permalinks

这个网站对我学习如何使用add\\u过滤器重写永久链接非常有帮助,可以解决我们将两个博客合并为一个博客时遇到的问题。但是,我确实有一件物品我找不到任何提示:在基于日期的存档链接上,即www.domain。com/2011/12/我只是想把它们翻译成:www.domain。com/news/2011/12/提前感谢您的帮助。