如何将域指向WordPress页面?

时间:2015-09-18 14:57:29

标签: php wordpress .htaccess

我有一个WordPress页面,其结构为http://mywebsite.com/page

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

如何实现这一目标?我虽然关于VirtualHosts,但由于文件夹/页面并不真正存在于服务器中,而是由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添加到主网址,但它无法正常工作,因为它会显示来自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的内容,但它也会更改我的网址http://mywebsite.com.br/page,这不是我正在寻找的内容,因为我想保留用户&#39 ;浏览器位于http://otherdomain.com.br

4 个答案:

答案 0 :(得分:1)

除了所有搜索引擎优化和重复内容问题:

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

添加以下过滤器和功能会使所需的页面显示为主页,而网址中没有/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;
}

答案 1 :(得分:0)

您需要添加重写规则。

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

答案 2 :(得分:0)

你不应该用[L,R = 301]将用户重定向到mywebsite.com,而应该使用P标志,它允许你让mod_rewrite充当代理对于域名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相比,这会降低性能,因为它不处理持久连接或连接池。

答案 3 :(得分:0)

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

基本上,您需要使用an external redirect中的otherdomainmywebsite,然后an internal redirectmywebsiteotherdomain。但它可能会导致循环,因此您需要使用某些条件来逃避它。

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]