【问题标题】:How to point a domain to a WordPress page?如何将域指向 WordPress 页面?
【发布时间】:2015-09-18 14:57:29
【问题描述】:

我有一个这种结构的 WordPress 页面 http://mywebsite.com/page

我希望我的http://otherdomain.com 指向http://mywebsite.com/page,但用户应该在他的浏览器中看到http://otherdomain.com 域?我的 DNS 中的 A 记录将 http://otherdomain.comhttp://mywebsite.com 指向同一个 IP。

如何实现这个目标?我虽然是关于 VirtualHosts,但由于文件夹 /page 在服务器中并不真正存在,而是由 WordPress 通过 .htaccess 动态创建的

如何将我的域指向 WordPress 页面?

在 Roel 回答之后,我在 WordPress Bitnami 安装中编辑了我的 htaccess.conf 文件以添加以下内容:

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^otherdomain\.com.br$
    RewriteRule (.*) http://www.otherdomain.com.br/$1 [R=301,L]
    RewriteRule ^$ http://mywebsite.com.br/page/ [L,R=301]

正在调用该代码,因为它将www 添加到主URL,但它不起作用,因为它显示来自mywebsite.com.br 而不是来自mywebsite.com.br/page 的内容。

我尝试了另一个代码

    RewriteEngine on
    RewriteCond %{HTTP_HOST} otherdomain\.com.br [NC]
    RewriteCond %{REQUEST_URI} ^/$
    RewriteRule ^(.*)$ http://mywebsite.com.br/page/$1 [L]

在这种情况下,它会显示来自 http://mywebsite.com.br/page 的内容,但是它也会更改我的 URL http://mywebsite.com.br/page,这不是我想要的,因为我想将用户的浏览器保持在 http://otherdomain.com.br

【问题讨论】:

  • mywebsite.comotherdomain.com 是否共享同一个文档根目录?
  • 我将 otherdomain.com 指向与 mywebsite.com 指向的 CNAME 别名相同
  • 你的意思是如果你去otherdomain.commywebsite.com你会得到相同的页面内容?
  • 没有。我希望如果我去otherdomain.com 我从mywebsite.com/page/ 获取内容而不更新URL。但是,/page/ 不是服务器中的文件夹,而是 WordPress 漂亮的 URL
  • 好吧,这不是我的问题。我的意思是now,如果你去otherdomain.com,你的内容和mywebsite.com一样吗?

标签: php wordpress .htaccess


【解决方案1】:

抛开所有 SEO 和重复内容问题:

WordPress 知道它应该在数据库中回答的域。 WordPress 网站本身将根据 WordPress 仪表板中设置的域名进行重定向(在“设置”=>“常规”下)。

这在使用 htaccess 时效果不佳。您实际上希望域名http://otherdomain.com 指向mywebsite.com 所在的服务器(不使用转发,而是修改DNS 中的A 记录)。原因是当你重写域名时,WordPress 站点也会在该域中查找 css 和 js 文件(在 otherdomain.com 处查找它们)。如果您将域指向 WordPress 服务器,这将正常工作。

为了使 WordPress 允许“回答”多个域,您需要修改 WordPress 安装中的代码,使其不会将 url 重写为 http://mywebsite.com。为此,请将以下 PHP 代码添加到 WordPress 主题的 function.php 文件中:

您需要做的是将过滤器添加到返回站点 URL 的相关挂钩中,以便您可以根据需要对其进行修改:

// This will allow you to modify the site url that is stored in the db when it is requested by WP
add_filter('option_siteurl', 'my_custom_filter_siteurl', 1);

// This will allow you to modify the home page url that is stored in the db when it is requested by WP
add_filter('option_home', 'my_custom_filter_home', 1);

// Modify the site url that WP gets from the database if appropriate
function my_custom_filter_siteurl( $url_from_settings ) {
    // Call our function to find out if this is a legit domain or not
    $current_domain = my_custom_is_domain_valid();
    // If so, use it
    if ( $current_domain ) {
        return "http://{$current_domain}";
        // Otherwise, use the default url from settings
    } else {
        return $url_from_settings;
    }
}

// Modify the home page url that WP gets from the database if appropriate
function my_custom_filter_home( $home_from_settings ) {
    // Call our function to find out if this is a legit domain or not
    $current_domain = my_custom_is_domain_valid();
    // If so, use it
    if ( $current_domain ) {
        $base_url = "http://{$current_domain}";
        // Modify / remove this as appropriate.  Could be simply return $base_url;
        return $base_url . "/home-page";
    // Otherwise, use the default url from settings
    } else {
        return $home_from_settings;
    }
}

// Utility function to determine URL and whether valid or not
function my_custom_is_domain_valid() {
    // Create a whitelist of valid domains you want to answer to
    $valid = array(
        'otherdomain.com',
        'mywebsite.com.br',
    );

    // Get the current domain name
    $current_server = $_SERVER['SERVER_NAME'];

    // If it's a whitelisted domain, return the domain
    if ( in_array( $current_server, $valid ) ) {
        return $current_server;
    }

    // Otherwise return false so we can use the default set in the DB
    return FALSE;
}

添加以下过滤器和函数将导致所需页面显示为主页,URL 中没有/page

// This will allow you to modify if a page should be shown on the home page
add_filter('option_show_on_front', 'my_custom_show_on_front');

// This will allow you to modify WHICH page should be shown on the home page
add_filter('option_page_on_front', 'my_custom_page_on_front');

// Modify if a page should be shown on the home page
function my_custom_show_on_front( $default_value ) {
    // We only want to do this on specific domain
    $is_other_domain = my_custom_is_other_domain();
    // Ensure it's the domain we want to show the specific page for
    if ( $is_other_domain ) {
        return 'page';
        // Otherwise, use the default setting
    } else {
        return $default_value;
    }
}

// Modify WHICH should be shown on the home page
function my_custom_page_on_front( $default_value ) {
    // We only want to do this on specific domain
    $is_other_domain = my_custom_is_other_domain();
    // Ensure it's the domain we want to show the specific page for
    if ( $is_other_domain ) {
        // If it's the proper domain, set a specific page to show
        // Alter the number below to be the ID of the page you want to show
        return 5;
    } else {
        // Otherwise, use the default setting
        return $default_value;
    }
}

// Utility function to easily determine if the current domain is the "other" domain
function my_custom_is_other_domain() {
    $current_domain = my_custom_is_domain_valid();

    return ($current_domain == 'otherdomain.com') ? $current_domain : FALSE;
}

【讨论】:

  • 这似乎可以将 WordPress 与多个域集成。但是,它并没有解决我的问题,我希望当用户转到 otherdomain.com 时,它会提供来自 mywebsite.com.br/page 的内容,而不会将 /page 添加到 URL 中
  • @JoãoPauloApolinárioPassos - 除非您将 mywebsite.com.br/page 设置为您在 WordPress 中的首页(在“设置”=>“阅读”下,顶部设置是“ Front Page Displays”。将其更改为“A Static Page”,然后选择 /page 页面。
  • 但只有当我从otherdomain.com 到达那里时,它才应该是首页。如果我从mywebsite.com.br 到达那里,首页应该是其他的。这可能吗?
  • @JoãoPauloApolinárioPassos - 让我看看能否为您找到一个过滤器。给我几个小时。
  • @JoãoPauloApolinárioPassos - 我已经更新了答案,包括两个新过滤器和三个新功能。这些过滤器/功能将导致主页显示所需的页面,而 url 中没有 /page。如果您有任何问题,请查看并告诉我。
【解决方案2】:

您需要添加重写规则。

RewriteEngine on
RewriteCond %{HTTP_HOST} ^otherdomain\.com$
RewriteRule (.*) http://www.otherdomain.com/$1 [R=301,L]
RewriteRule ^$ http://mywebsite.com/page [L,R=301]

【讨论】:

  • 我尝试在 WordPress 安装文件夹的 .htaccess 页面上执行此操作,但没有成功。我现在用我的代码编辑了问题
  • 您应该将此添加到 wordpress 安装但添加到 virtualhost 或虚拟主机的 .htaccess
  • 我在 Bitnami,正确的路径是 /opt/bitnami/apps/wordpress/conf/htaccess.conf。现在调用了代码,但它不起作用。当我键入但不显示来自 mywebsite.com/page 的内容时,它将www 添加到 otherdomain.com,仅显示 mywebsite.com
【解决方案3】:

您不应该使用 [L,R=301] 将用户重定向到 mywebsite.com,而是应该使用允许您要求 mod_rewrite 充当 代理 的 P 标志对于域 otherdomain.com。像这样的,

RewriteRule /(.*) http://mywebsite.com/page/$1 [P]

确保将 css 文件也更改为以下内容,

RewriteRule /css/(.*) http://mywebsite.com/css/$1 [P]

这将允许您的所有 otherwebsite.com css 成为 mywebsite.com css 并显示为 otherwebsite.com/css 而不是 mywebsite.com/page/css

但为了做到这一点,您需要加载并启用 mod_proxy。但是请注意,与直接使用 mod_proxy 相比,这会降低性能,因为它不处理持久连接或连接池。

【讨论】:

  • 问题是mywebsite.com/page不是服务器中真正的文件夹。这不像我喜欢/var/www/page。这是一个WordPress漂亮的URL自生成页面,所以我认为CSS部分没有意义
【解决方案4】:

试试这个正则表达式。它可能不起作用,但它会让您知道在哪里可以找到解决方案。

基本上你需要从otherdomainmywebsite使用an external redirect,然后从mywebsiteotherdomain使用an internal redirect。但它可能会导致一个循环,所以你需要使用一些条件来逃避它。

RewriteEngine on

RewriteCond %{ENV:REDIRECT_FINISH} !^$
RewriteRule ^ - [L]

RewriteCond %{HTTP_HOST} ^otherdomain\.com.br$ [NC]
RewriteRule ^(.*)$ http://mywebsite.com.br/page/$1 [R,L]

RewriteRule ^.*$ http://otherdomain.com.br [E=FINISH:1]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-10
    • 1970-01-01
    • 2015-04-18
    • 1970-01-01
    • 1970-01-01
    • 2022-08-21
    • 1970-01-01
    • 2023-03-24
    相关资源
    最近更新 更多