admin管理员组文章数量:1122846
I have two domains uat1.example
and uat2.example
pointing to the same wordpress installation.
I want to change the home_url and site_url for all links on the website to uat2.example
, but it should not redirect to uat2.example
if the request comes on uat1.example
,
So suppose someone visits my website, uat1.example; it should load it. But all links on the web page then should point to uat2.example
. So if the User lands on the page using uat1.example
and clicks on any link it will send the user to uat2.example
; that is ok.
I tried putting this in config:
define('WP_HOME','');
define('WP_SITEURL','');
but when I visit uat1.example
it is redirecting me to uat2.example
, which is obvious. but I don't want that.
Do we have any trick so that home_url and site_url remains uat2.example
only? But when I visit uat1.example
, it doesn't redirect me to uat1.example
and still consider uat2.example
as home_url for all links on that page?
I have two domains uat1.example.com
and uat2.example.com
pointing to the same wordpress installation.
I want to change the home_url and site_url for all links on the website to uat2.example.com
, but it should not redirect to uat2.example.com
if the request comes on uat1.example.com
,
So suppose someone visits my website, uat1.example.com; it should load it. But all links on the web page then should point to uat2.example.com
. So if the User lands on the page using uat1.example.com
and clicks on any link it will send the user to uat2.example.com
; that is ok.
I tried putting this in config:
define('WP_HOME','http://uat2.example.com');
define('WP_SITEURL','http://uat2.example.com');
but when I visit uat1.example.com
it is redirecting me to uat2.example.com
, which is obvious. but I don't want that.
Do we have any trick so that home_url and site_url remains uat2.example.com
only? But when I visit uat1.example.com
, it doesn't redirect me to uat1.example.com
and still consider uat2.example.com
as home_url for all links on that page?
2 Answers
Reset to default 0You need to set the WP_SITEURL
and WP_HOME
in wp-config.php
file.
Please add below code.
define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST']);
define('WP_HOME', 'http://' . $_SERVER['HTTP_HOST']);
In your server SSL setup then used below code
define('WP_SITEURL', 'https://' . $_SERVER['HTTP_HOST']);
define('WP_HOME', 'https://' . $_SERVER['HTTP_HOST']);
To achieve the behavior you're looking for—where users accessing uat1.example.com are not redirected but all links on the site point to uat2.example.com—you'll need to use a more dynamic approach rather than hardcoding the home_url and site_url in the wp-config.php. You can accomplish this using a filter in your functions.php file.
Here’s how you can do it:
Remove the define statements from wp-config.php:
Make sure you don't have the WP_HOME and WP_SITEURL constants set in your wp-config.php because these are causing the redirect behavior.
Add a filter in functions.php to dynamically change home_url and site_url:
You can use the home_url and site_url filters to change these URLs dynamically based on the current domain:
add_filter('home_url', 'custom_home_url', 10, 2);
add_filter('site_url', 'custom_site_url', 10, 4);
function custom_home_url($url, $path, $orig_scheme, $blog_id) {
// Check if the current domain is uat1.example.com
if ($_SERVER['HTTP_HOST'] === 'uat1.example.com') {
// Replace the host in the URL to point to uat2.example.com
$url = str_replace('uat1.example.com', 'uat2.example.com', $url);
}
return $url;
}
function custom_site_url($url, $path, $scheme, $blog_id) {
// Check if the current domain is uat1.example.com
if ($_SERVER['HTTP_HOST'] === 'uat1.example.com') {
// Replace the host in the URL to point to uat2.example.com
$url = str_replace('uat1.example.com', 'uat2.example.com', $url);
}
return $url;
}
home_url
Filter: This filter is applied whenever home_url()
is called in WordPress, which generates the home URL of your site.
site_url Filter: Similarly, the site_url()
filter is used for generating the site URL.
Both functions check if the current domain is uat1.example.com and replace it with uat2.example.com for all generated URLs. However, the page itself will continue to load on uat1.example.com without redirecting.
本文标签: site urlchange homeurl and siteurl but don39t redirect to homeurl and siteurl on load
版权声明:本文标题:site url - change home_url and site_url but don't redirect to home_url and site_url on load 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736288256a1928056.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论