2015-09-18 128 views
2

我有一个WordPress的页面,这种结构http://mywebsite.com/page如何将域指向WordPress页面?

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

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

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

罗埃尔答案后,我编辑我htaccess.conf文件在WordPress的Bitnami安装补充一点:

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内容然而,它改变了我的网址http://mywebsite.com.br/page,以及这是不是我要找的,因为我想保留用户的浏览器在http://otherdomain.com.br

+0

做'mywebsite.com'和' otherdomain.com'共享相同的文档根目录? –

+0

我指出'otherdomain.com'为与mywebsite.com相同的CNAME别名 – steps

+0

你的意思是说,如果你去'otherdomain.com'或'mywebsite.com',你会得到相同的页面内容? –

回答

1

所有的搜索引擎优化和一边重复内容的问题:

WordPress是意识到域的它应该在数据库中回答到。 WordPress网站本身将重定向基于WordPress仪表板(在“设置”=>“常规”)下设置的域名。

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

为了使WordPress允许“回答”多个域,您需要修改WordPress安装中的代码,以便它不会将网址重写为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; 
} 
+0

这似乎将WordPress与多个域进行整合。然而,它并没有解决我的问题,我想这是当用户去'otherdomain.com'它提供'mywebsite.com.br/page'的内容而不在URL中添加'/ page' – steps

+0

@JoãoPauloApolinárioPassos - 除非您将WordPress的“mywebsite.com.br/page”设置为您的首页(在“设置”=>“阅读”下,顶部设置为“前页面显示”),否则这件作品无法解决。 。将其改为“静态页面”,然后选择“/ page”页面。 –

+0

但是,如果我从'otherdomain.com'到达那里,它应该是首页。如果我从'mywebsite.com.br'到达那里,头版应该是其他的。这可能吗? – steps

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] 
+0

我试图在WordPress安装文件夹的.htaccess页面执行此操作,但没有成功。 我现在用我的代码编辑了这个问题 – steps

+0

您应该将此添加到wordpress安装中,但添加到虚拟主机的虚拟主机或.htaccess中 –

+0

我在Bitnami,正确的路径是/ opt/bitnami/apps/wordpress/conf/htaccess.conf'。代码现在被调用,但它不起作用。 当我输入“www”时,它会将“www”添加到otherdomain.com,但不显示mywebsite.com/page中的内容,仅mywebsite.com – steps

0

你不应该与用户重定向到mywebsite.com [L,R = 301],相反,你应该使用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相比,这会降低性能,因为它不处理持久连接或连接池。

+0

问题是“mywebsite.com/page”不是真正的文件夹服务器。这不像是我喜欢/ var/www/page。这是一个WordPress漂亮的URL自生页面,所以我认为CSS部分没有意义 – steps

0

试试这个正则表达式。它可能不起作用,但它会给你一些想法,以找到解决方案的位置。

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

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] 
相关问题