有没有办法从wordpress中的链接生成中排除域 [英] Is there a way to exclude domain from link generation in wordpress

查看:36
本文介绍了有没有办法从wordpress中的链接生成中排除域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个响应 *.domain.com 的网站.

I have a website that responds on *.domain.com.

转到 x.domain.comy.domain.com 应该会生成相同的网页.

Going to x.domain.com or y.domain.com should produce the same web page.

我不知道 * 是什么,但它是重要的信息,因为我们根据它跟踪事物.

What * is I do not know, but it is and important piece of info since we track things based on it.

在迁移到 wordpress 时,我们遇到了一个非常严重的问题.它似乎与在管理员中设置的域生成链接(使用 get_page_link).

When moving to wordpress we ran into a pretty severe problem. It seems to generate links (using get_page_link) with the domain which is set in the admin.

这对我们不起作用,因为我们无法找到一种方法来告诉 wordpress 生成没有域的链接(为什么它会这样做?!)并且每次单击链接时,浏览器都会从:x.domain.comdomain.com(因为 domain.com 是我们在管理员中拥有的).

This will not work for us because we can't find a way to tell wordpress to generate links without the domain (why does it do this anyway?!) and every time a link is clicked the browser goes from: x.domain.com to domain.com (since domain.com is what we have in the admin).

推荐答案

不幸的是,WordPress 的架构很难摆脱 URL 的域组件.但一切都没有丢失!请继续阅读,因为您的问题的答案需要一些背景知识.

Unfortunately WordPress is architected such that it is really hard to get rid of the domain component of URLs. But all is not lost! Read on as the answer to your question requires a bit of background.

WordPress 团队决定要求站点用户通过管理控制台(您可以在以下屏幕截图中看到)或通过 PHP 在数据库中硬编码站点域,我们将在下面讨论:

The WordPress team made the decision to require the user of a site to hardcode the site domain either in the database via an admin console, which you can see in the following screen shot, of via PHP which we'll discuss below:

您可能会问这两个 URL 之间有什么区别?即使我觉得这很令人困惑,因为我几乎不需要任何其他东西来将它们都设置为根 URL,并且因为这对您的问题并不重要,所以我将忽略该细节.如果您有兴趣,可以在此处了解更多信息:

You might ask what the difference is between the the two URLs? Even I find it confusing because I almost never need anything other them to have them both set to the root URL and as it's not important for your question I'll just gloss over that detail. If you are interested though you can learn more here:

  • Changing The Site URL
  • Editing wp-config.php: WordPress Address (URL)
  • Giving WordPress Its Own Directory

继续,另一个选项是在 /wp-config.php 文件中硬编码两个 PHP 常量 WP_SITEURL 和 WP_HOME,该文件可以在 WordPress 安装的根目录中找到.这两行在您的 /wp-config.php 文件中可能如下所示:

Moving along, the other option is to hardcode the two PHP constants WP_SITEURL and WP_HOME in the /wp-config.php file which can be found in the root of a WordPress installation. Those two lines might look like this in your /wp-config.php file:

define('WP_HOST','http://domain.com');
define('WP_SITEURL','http://domain.com');

好消息是,您可以根据您的站点提供服务的当前域动态定义它们(我假设您同时拥有DNS 服务器和为 通配 DNS 配置的 Apache Web 服务器.)您可以使用以下代码匹配任何由字母和数字组成的子域名:

The good news is that you can define both of them dynamically based on the current domain your site is serving from (I'm going to assume you have both your DNS server and your Apache web server configured for wildcard DNS.) You can use the following code to match any subdomain name consisting of letters and numbers:

$root_domain = 'domain.com';  // Be sure to set this to your 2nd level domain!!!
$this_domain = $_SERVER['SERVER_NAME'];
if (!preg_match("#^([a-zA-Z0-9]+\.)?{$root_domain}$#",$this_domain)) {
    echo "ERROR: The domain [$this_domain] is not a valid domain for this website.";
    die();
} else {
    define('WP_HOME',"http://{$this_domain}");
    define('WP_SITEURL',"http://{$this_domain}");
}

坏消息是你可能有一些工件"在你让它工作后处理例如如何处理存储在数据库内容中的图像 URL(这可能会或可能不会成为问题)或 Google Maps API 密钥等的 URL.如果您遇到问题,让我建议您在此处发布另一个问题或在新的 WordPress Answers Exchange 中甚至更好,它也由与 StackOverflow 相同的人运营.

The bad news is you may have some "artifacts" to deal with after you get it working such as how URLs are handled for image URLs stored in the database content (which may or may not end up being a problem) or for Google Maps API keys, etc. If you have trouble with them let me suggest you post another question here or even better at the new WordPress Answers Exchange also run by the same people as StackOverflow.

至于告诉 WordPress 如何生成链接,您可以hook"过滤器,但在我的快速测试中,我认为您不需要它,因为 WordPress 会为发生的任何域生成链接成为您当前的域.尽管如此,如果你确实发现你需要它们,你可以这样做,尽管准备好被所有所需的 add_filter() 语句所淹没!每一个都控制着在 WordPress 中生成链接的不同方式之一.

As for telling WordPress how to generate links, there are filters you can "hook" but in my quick testing I don't think you need it because WordPress will generate the links for whatever domain happens to be your current domain. Still if you do find you need them you can do it although be prepared to be overwhelmed by all the add_filter() statements required! Each one controls one of the different ways links can be generated in WordPress.

这是钩子过滤器函数和 40 多个 add_filter() 调用;你可能不需要它们,但如果你在这里使用它们:

Here is the hook filter function and the 40+ add_filter() calls; you might not need them all but if you do here they are:

function multi_subdomain_permalink($permalink){
    $root_domain = 'domain.com';
    $this_domain = $_SERVER['SERVER_NAME'];
    if (preg_match("#^([a-zA-Z0-9]+)\.?{$root_domain}$#",$this_domain,$match)) {
        $permalink = str_replace("http://{$match[1]}.",'http://',$permalink);
    }
    return $permalink;
}
add_filter('page_link','multi_subdomain_permalink');
add_filter('post_link','multi_subdomain_permalink');
add_filter('term_link','multi_subdomain_permalink');
add_filter('tag_link','multi_subdomain_permalink');
add_filter('category_link','multi_subdomain_permalink');
add_filter('post_type_link','multi_subdomain_permalink');
add_filter('attachment_link','multi_subdomain_permalink');
add_filter('year_link','multi_subdomain_permalink');
add_filter('month_link','multi_subdomain_permalink');
add_filter('day_link','multi_subdomain_permalink');
add_filter('search_link','multi_subdomain_permalink');

add_filter('feed_link','multi_subdomain_permalink');
add_filter('post_comments_feed_link','multi_subdomain_permalink');
add_filter('author_feed_link','multi_subdomain_permalink');
add_filter('category_feed_link','multi_subdomain_permalink');
add_filter('taxonomy_feed_link','multi_subdomain_permalink');
add_filter('search_feed_link','multi_subdomain_permalink');

add_filter('get_edit_tag_link','multi_subdomain_permalink');
add_filter('get_edit_post_link','multi_subdomain_permalink');
add_filter('get_delete_post_link','multi_subdomain_permalink');
add_filter('get_edit_comment_link','multi_subdomain_permalink');
add_filter('get_edit_bookmark_link','multi_subdomain_permalink');

add_filter('index_rel_link','multi_subdomain_permalink');
add_filter('parent_post_rel_link','multi_subdomain_permalink');
add_filter('previous_post_rel_link','multi_subdomain_permalink');
add_filter('next_post_rel_link','multi_subdomain_permalink');
add_filter('start_post_rel_link','multi_subdomain_permalink');
add_filter('end_post_rel_link','multi_subdomain_permalink');

add_filter('previous_post_link','multi_subdomain_permalink');
add_filter('next_post_link','multi_subdomain_permalink');

add_filter('get_pagenum_link','multi_subdomain_permalink');
add_filter('get_comments_pagenum_link','multi_subdomain_permalink');
add_filter('shortcut_link','multi_subdomain_permalink');
add_filter('get_shortlink','multi_subdomain_permalink');

add_filter('home_url','multi_subdomain_permalink');
add_filter('site_url','multi_subdomain_permalink');
add_filter('admin_url','multi_subdomain_permalink');
add_filter('includes_url','multi_subdomain_permalink');
add_filter('content_url','multi_subdomain_permalink');
add_filter('plugins_url','multi_subdomain_permalink');

add_filter('network_site_url','multi_subdomain_permalink');
add_filter('network_home_url','multi_subdomain_permalink');
add_filter('network_admin_url','multi_subdomain_permalink');

虽然将我们带到了最后一点.WordPress 中的功能试图确保加载的每个 URL 都通过其 canonical URL 通常是一种网络最佳实践,特别是如果您希望在 Google 和其他搜索引擎上优化搜索引擎结果.但是,就您而言,如果您确实希望 WordPress 重定向到您的规范 URL,那么您需要添加一个 redirect_canonical 过滤器钩子和告诉 WordPress 不要这样做.

While brings us to the final point. There is functionality in WordPress that attempts to ensure every URL that is loaded is served via its canonical URL which in general is a web best practice, especially if you are concerned with optimizing search engine results on Google and other search engines. In your case, however, if you really do not want WordPress to redirect to your canonical URL then you need to add a redirect_canonical filter hook and tell WordPress not to do it.

接下来是确保任何用作x.domain.com"的页面都保留在x.domain.com"上的代码,即使所有URL 被过滤为domain.com".这可能不是您需要的确切逻辑,但我只是向您展示 WordPress 的构建块,以便您能够找出所需的逻辑.

What follows is the code to make sure any page that serves as "x.domain.com" stays on "x.domain.com" even if all the URLs are filtered to be "domain.com". That may not be the exact logic you need but I'm just showing you the building blocks of WordPress so you'll be able to figure out the logic that you require.

关于这个函数调用的一些最终细节;参数 #3 和 #4 分别指的是 priority(10 是标准优先级,所以这个钩子不会被特殊处理)和 函数参数的数量(这 2 个参数是$redirect_url$requested_url.)另外要注意的是返回 false 而不是有效的 URL 会取消规范重定向:

A few final details about this function call; parameters #3 and #4 refer respectively to the priority (10 is standard priority so this hook will not be handled special) and the number of function arguments (the 2 arguments are $redirect_url and $requested_url.) The other thing to note is that returning false instead of a valid URL cancels the canonical redirect:

add_filter('redirect_canonical','multi_subdomain_redirect_canonical',10,2);
function multi_subdomain_redirect_canonical($redirect_url,$requested_url){
    $redirect = parse_url($redirect_url);
    $requested = parse_url($requested_url);
    // If the path+query is the same for both URLs, Requested and Redirect, and
    if ($redirect['path']+$redirect['query']==$requested['path']+$requested['query']) {
            // If Requested URL is a subdomain of the Redirect URL
        if (preg_match("#^([a-zA-Z0-9]+).{$redirect['host']}$#",$requested['host'])) {
            $redirect_url = false;  // Then cancel the redirect
        }
    }
    return $redirect_url;
}

就是这样.希望这会有所帮助.

That's about it. Hope this helps.

-迈克

这篇关于有没有办法从wordpress中的链接生成中排除域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆