Is there a way to exclude a domain from link building in wordpress

I have a website that responds to * .domain.com.

Going to x.domain.comor y.domain.comshould produce the same web page.

What I don’t know, but this is also important information, since we monitor everything based on it.

In the transition to wordpress, we encountered a rather serious problem. It seems to be creating links (using get_page_link) with the domain set to admin.

This will not work for us, because we cannot find a way to say wordpress to create links without a domain (why is it anyway ?!), and every time a link is clicked, the browser goes from: x.domain.comto domain.com(since domain.com is what we have in the admin panel).

+3
source share
2 answers

Unfortunately, WordPress is archived in such a way that it is very difficult to get rid of the domain component of the URL. But all is not lost! Read on as the answer to your question requires a bit of background.

The WordPress team decided to require the site user to hardcode the domain of the site or in the database through the admin console, which you can see in the following screenshot, through PHP, which we will talk about below:

WP_HOME and WP_SITEURL in WordPress Admin Console

You may ask, what is the difference between the two URLs? Even I find this confusing because I almost never need something else for both of them to set the root URL, and since this is not important for your question, I will simply mask this detail. If you are interested, you can find out more here:

, , PHP WP_SITEURL WP_HOME /wp-config.php, WordPress. /wp-config.php:

define('WP_HOST','http://domain.com');
define('WP_SITEURL','http://domain.com');

, , ( , DNS-, - Apache DNS.) , :

$root_domain = 'domain.com';  // Be sure to set this to your 2nd level domain!!!
$this_domain = $_SERVER['SERVER_NAME'];
if (!preg_match("#^([a-zA-Z0-9]+\.)?{$root_domain}$#",$this_domain)) {
    echo "ERROR: The domain [$this_domain] is not a valid domain for this website.";
    die();
} else {
    define('WP_HOME',"http://{$this_domain}");
    define('WP_SITEURL',"http://{$this_domain}");
}

. "", , , , URL- URL- , ( ) API Google .. , WordPress Answers Exchange , StackOverflow.

WordPress, , , "", , , WordPress , . , , , , , add_filter()! , WordPress.

40+ add_filter(); , , :

function multi_subdomain_permalink($permalink){
    $root_domain = 'domain.com';
    $this_domain = $_SERVER['SERVER_NAME'];
    if (preg_match("#^([a-zA-Z0-9]+)\.?{$root_domain}$#",$this_domain,$match)) {
        $permalink = str_replace("http://{$match[1]}.",'http://',$permalink);
    }
    return $permalink;
}
add_filter('page_link','multi_subdomain_permalink');
add_filter('post_link','multi_subdomain_permalink');
add_filter('term_link','multi_subdomain_permalink');
add_filter('tag_link','multi_subdomain_permalink');
add_filter('category_link','multi_subdomain_permalink');
add_filter('post_type_link','multi_subdomain_permalink');
add_filter('attachment_link','multi_subdomain_permalink');
add_filter('year_link','multi_subdomain_permalink');
add_filter('month_link','multi_subdomain_permalink');
add_filter('day_link','multi_subdomain_permalink');
add_filter('search_link','multi_subdomain_permalink');

add_filter('feed_link','multi_subdomain_permalink');
add_filter('post_comments_feed_link','multi_subdomain_permalink');
add_filter('author_feed_link','multi_subdomain_permalink');
add_filter('category_feed_link','multi_subdomain_permalink');
add_filter('taxonomy_feed_link','multi_subdomain_permalink');
add_filter('search_feed_link','multi_subdomain_permalink');

add_filter('get_edit_tag_link','multi_subdomain_permalink');
add_filter('get_edit_post_link','multi_subdomain_permalink');
add_filter('get_delete_post_link','multi_subdomain_permalink');
add_filter('get_edit_comment_link','multi_subdomain_permalink');
add_filter('get_edit_bookmark_link','multi_subdomain_permalink');

add_filter('index_rel_link','multi_subdomain_permalink');
add_filter('parent_post_rel_link','multi_subdomain_permalink');
add_filter('previous_post_rel_link','multi_subdomain_permalink');
add_filter('next_post_rel_link','multi_subdomain_permalink');
add_filter('start_post_rel_link','multi_subdomain_permalink');
add_filter('end_post_rel_link','multi_subdomain_permalink');

add_filter('previous_post_link','multi_subdomain_permalink');
add_filter('next_post_link','multi_subdomain_permalink');

add_filter('get_pagenum_link','multi_subdomain_permalink');
add_filter('get_comments_pagenum_link','multi_subdomain_permalink');
add_filter('shortcut_link','multi_subdomain_permalink');
add_filter('get_shortlink','multi_subdomain_permalink');

add_filter('home_url','multi_subdomain_permalink');
add_filter('site_url','multi_subdomain_permalink');
add_filter('admin_url','multi_subdomain_permalink');
add_filter('includes_url','multi_subdomain_permalink');
add_filter('content_url','multi_subdomain_permalink');
add_filter('plugins_url','multi_subdomain_permalink');

add_filter('network_site_url','multi_subdomain_permalink');
add_filter('network_home_url','multi_subdomain_permalink');
add_filter('network_admin_url','multi_subdomain_permalink');

. WordPress , , URL- canonical URL, ​​, Google . , , , WordPress URL-, redirect_canonical WordPress, .

, , , "x.domain.com", "x.domain.com", URL- "domain.com". , , WordPress, .

; # 3 # 4 (10 - , ) (2 - $redirect_url $requested_url.) false URL- :

add_filter('redirect_canonical','multi_subdomain_redirect_canonical',10,2);
function multi_subdomain_redirect_canonical($redirect_url,$requested_url){
    $redirect = parse_url($redirect_url);
    $requested = parse_url($requested_url);
    // If the path+query is the same for both URLs, Requested and Redirect, and
    if ($redirect['path']+$redirect['query']==$requested['path']+$requested['query']) {
            // If Requested URL is a subdomain of the Redirect URL
        if (preg_match("#^([a-zA-Z0-9]+).{$redirect['host']}$#",$requested['host'])) {
            $redirect_url = false;  // Then cancel the redirect
        }
    }
    return $redirect_url;
}

. , .

-Mike

+7

- ? , rewrite apache, apache.

httpd.conf :

RewriteEngine On
RewriteCond %{HTTP_HOST} ^x\.domain\.com
RewriteRule ^(.*)$ http://www.domain.com/x/$1
RewriteCond %{HTTP_HOST} ^y\.domain\.com
RewriteRule ^(.*)$ http://www.domain.com/y/$1

"v"

RewriteRule ^(.*)$ http://www.domain.com/$1&var=v

, , , , - , .

.

.

+1

Source: https://habr.com/ru/post/1759422/


All Articles