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/page
however 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;
}
You'll need to add rewrite rules.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^otherdomain\.com$
RewriteRule (.*) http://www.otherdomain.com/$1 [R=301,L]
RewriteRule ^$ http://mywebsite.com/page [L,R=301]
You shouldn't be redirecting the user to mywebsite.com with [L,R=301], rather, you should use the P flag which allows you to ask mod_rewrite to act as a proxy for the domain otherdomain.com. something like this,
RewriteRule /(.*) http://mywebsite.com/page/$1 [P]
make sure to change the css files too to the following,
RewriteRule /css/(.*) http://mywebsite.com/css/$1 [P]
which will allow all your otherwebsite.com css to become mywebsite.com css and display as otherwebsite.com/css rather than mywebsite.com/page/css
but in order to do this, you need to have mod_proxy loaded and enabled. Do however note that this will reduce performance compared to using mod_proxy directly, as it doesn't handle persistent connections or connection pooling.
Try this regex. It may not work, but it will give you some idea on where to find the solution.
Basically you need to use an external redirect
from otherdomain
to mywebsite
and then an internal redirect
from mywebsite
to otherdomain
. But it may cause a loop so you need to use some condition to escape it.
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]