Php 如何将域指向WordPress页面?
我有一个具有这种结构的WordPress页面Php 如何将域指向WordPress页面?,php,wordpress,.htaccess,Php,Wordpress,.htaccess,我有一个具有这种结构的WordPress页面http://mywebsite.com/page 我想要我的http://otherdomain.com指向http://mywebsite.com/page但是用户应该看到http://otherdomain.comdomain在他的浏览器中?我的DNS中的A记录指向http://otherdomain.com和http://mywebsite.com到同一IP 如何实现这一目标?我想是关于VirtualHosts的,但因为文件夹/页面实际上并不存
http://mywebsite.com/page
我想要我的http://otherdomain.com
指向http://mywebsite.com/page
但是用户应该看到http://otherdomain.com
domain在他的浏览器中?我的DNS中的A记录指向http://otherdomain.com
和http://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
添加到主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
但是它改变了我的URLhttp://mywebsite.com.br/page
这也不是我想要的,因为我想让用户的浏览器保持在http://otherdomain.com.br
您需要添加重写规则
RewriteEngine on
RewriteCond %{HTTP_HOST} ^otherdomain\.com$
RewriteRule (.*) http://www.otherdomain.com/$1 [R=301,L]
RewriteRule ^$ http://mywebsite.com/page [L,R=301]
您不应该使用[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_代理。但是请注意,与直接使用mod_代理相比,这将降低性能,因为它不处理持久连接或连接池 试试这个正则表达式。它可能不起作用,但它会让你知道在哪里可以找到解决方案 基本上,您需要使用
外部重定向
从其他域
到我的网站
,然后使用内部重定向
从我的网站
到其他域
。但它可能会导致循环,所以您需要使用一些条件来逃避它
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]
除了所有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安装文件夹的.htaccess页面上执行此操作,但没有成功。我现在用我的代码编辑了这个问题。你应该将它添加到wordpress安装中,但添加到virtualhost或.htaccess中。我在Bitnami,正确的路径是
/opt/Bitnami/apps/wordpress/conf/htaccess.conf
。代码现在已被调用,但它不起作用。当我输入mywebsite.com/page中的内容时,它会将www
添加到otherdomain.com,但不会显示其中的内容,只有mywebsite.commywebsite.com
和otherdomain.com
共享相同的文档根?我将otherdomain.com
指向与mywebsite.com
指向的CNAME别名相同的CNAME别名。您的意思是,如果您转到otherdomain.com
或mywebsite.com
,您将获得相同的页面内容?否。如果我去otherdomain.com
我想在不更新URL的情况下从mywebsite.com/page/
获取内容。然而,/page/
不是服务器中的文件夹,而是WordPress漂亮的URL OK,这不是我的问题。我的意思是现在如果你去otherdomain.com
你有与mywebsite.com
相同的内容吗?问题是mywebsite.com/page
在服务器中不是一个真正的文件夹。这不像我有like/var/www/page。这是一个WordPress漂亮的URL自生成页面,所以我认为CSS部分没有意义这似乎可以将WordPress与多个域集成。然而,它没有解决我的问题,我希望当用户转到otherdomain.com
时,它提供mywebsite.com.br/page
中的内容,而不将/page
添加到URL@JoãopaulapolinárioPassos-除非你将mywebsite.com.br/page
设为头版,否则这篇文章是无法解决的在WordPress中(在“设置”=>“阅读”下),顶部设置为“首页显示”。将其更改为“静态页面”,并选择/Page
页面。但只有从otherdomain.com
到达首页时,它才应该是首页。如果我从mywebsite.com.br