如何将域指向WordPress页面? [英] How to point a domain to a WordPress page?

查看:68
本文介绍了如何将域指向WordPress页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我想要我的 http://otherdomain.com 指向 http://mywebsite.com/page ,但用户应在浏览器中看到 http://otherdomain.com 域?我的DNS中的A记录将 http://otherdomain.com http://mywebsite.com 指向相同的IP。



如何实现此目标?我虽然是关于VirtualHosts的,但由于文件夹/ p​​age在服务器中实际上并不存在,而是由WordPress通过.htaccess动态创建的。



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



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



<
上的pre> RewriteEngine 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

解决方案

除了所有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文件中:



您需要做的是向相关的挂钩添加过滤器返回网站网址,因此您可以根据需要对其进行修改:

  //这将允许您修改网站网址当WP 
add_filter('option_siteurl','my_custom_filter_siteurl',1)请求时,将其存储在数据库中;

// //当WP
请求add_filter(’option_home’,‘my_custom_filter_home’,1)时,将允许您修改存储在数据库中的主页URL。

//修改WP从数据库获取的站点网址
函数my_custom_filter_siteurl($ url_from_settings){
//调用我们的函数以查找这是否合法是否域
$ current_domain = my_custom_is_domain_valid();
//如果是这样,请使用它
if($ current_domain){
return http:// {$ current_domain};
//否则,请使用设置
中的默认网址} else {
return $ url_from_settings;
}
}

//修改WP从数据库获取的主页URL(如果合适)
函数my_custom_filter_home($ home_from_settings){
//调用我们的函数以查找这是否是合法域
$ current_domain = my_custom_is_domain_valid();
//如果是这样,请使用它
if($ current_domain){
$ base_url = http:// {$ current_domain};
//适当地修改/删除它。可以简单地返回$ base_url;
返回$ base_url。 /主页;
//否则,请使用设置
中的默认网址} else {
return $ home_from_settings;
}
}

//用于确定URL以及是否有效的实用函数
函数my_custom_is_domain_valid(){
//创建有效的白名单您要回答
$ valid =数组的域(
'otherdomain.com',
'mywebsite.com.br',
);

//获取当前域名
$ current_server = $ _SERVER [’SERVER_NAME’];

//如果是列入白名单的域,则返回域
if(in_array($ current_server,$ valid)){
return $ current_server;
}

//否则返回false,因此我们可以使用数据库中的默认设置
return FALSE;
}

添加以下过滤器和功能将使所需的页面显示为主页页面,URL中没有 / page

  //将允许您修改是否在首页上显示页面
add_filter('option_show_on_front','my_custom_show_on_front');

//这将允许您修改应该在首页上显示的页面
add_filter(’option_page_on_front’,‘my_custom_page_on_front’);

//修改是否在首页上显示页面
函数my_custom_show_on_front($ default_value){
//我们只想在特定域$ b $上执行此操作b $ is_other_domain = my_custom_is_other_domain();
// //如果($ is_other_domain){
return‘page’;如果这是我们要显示
特定页面的域,
//否则,使用默认设置
} else {
return $ default_value;
}
}

//修改应该在首页上显示
函数my_custom_page_on_front($ default_value){
//我们只想在特定域
$ is_other_domain = my_custom_is_other_domain()上执行此操作;
//确保它是我们要显示
的特定页面的域if($ is_other_domain){
//如果是正确的域,请将特定的页面设置为显示
//将下面的数字更改为要显示的页面的ID
return 5;
} else {
//否则,使用默认设置
return $ default_value;
}
}

//实用函数轻松确定当前域是否为其他域
函数my_custom_is_other_domain(){
$ current_domain = my_custom_is_domain_valid();

回报($ current_domain ==‘otherdomain.com’)吗? $ current_domain:否;
}


I have a WordPress page with this structure http://mywebsite.com/page

I want my http://otherdomain.com to point to http://mywebsite.com/page but the user should see the http://otherdomain.com domain at his browser? The A record in my DNS points http://otherdomain.com and http://mywebsite.com to the same IP.

How to achieve this goal? I though about VirtualHosts but since the folder /page doesn't really exist in the server but is created dynamically by WordPress via .htaccess

How can I point my domain to the WordPress page?

After Roel answer I edited my htaccess.conf file at the WordPress Bitnami installation to add this:

    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]

The code is being called, because it adds the www to the main URL, but it's not working, because it displays contents from mywebsite.com.br and not from mywebsite.com.br/page.

I tried another code which is

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

In this case, it display the contents from http://mywebsite.com.br/pagehowever it changes my URL http://mywebsite.com.br/page as well which is not what I'm looking for, since I want to keep the user's browser at http://otherdomain.com.br

解决方案

All SEO and duplicate content issues aside:

WordPress is aware of the domain it is supposed to be answering to in the database. The WordPress site itself will also redirect based on the domain name set in the WordPress dashboard (under "Settings" => "General").

This isn't going to work well using htaccess. You're actually going to want the domain name http://otherdomain.com to point to the server where mywebsite.com lives (not using a forward, but modifying the A record in your DNS). The reason is that when you rewrite the domain name, the WordPress site is also going to look for css and js files at that domain (looking for them at otherdomain.com). If you have the domain pointed to the WordPress server, this will work properly.

In order to cause WordPress to allow "answering" to multiple domains, you need to modify the code in your WordPress installation so that it will not rewrite the url to http://mywebsite.com. To do this, add the below PHP code to your WordPress theme's function.php file:

What you need to do is to add filter(s) to the relevant hooks that return the site URL, so you can modify it as you see fit:

// 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;
}

Adding the following filters and functions will cause the desired page to appear as the home page, without the /page in the URL:

// 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页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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