Different domain for the site section

There are two websites that let you call them a.com and b.com ; so far they are completely separate. Nevertheless, there is a “synergy”, so the client wants to make b.com part of a.com (the same media library, users, host, database and so on) ... but with it its own domain and design. A site inside a site with its own domain.

SO, basically: when a user navigates to a specific part of a site - say, a.com/b, the URL should be changed to b.com.

I would think that I can solve this using apache virtual hosts; I found this aswer for how it is with Yii: Assign a different domain for only two controllers

But I use Wordpress (already made the main site), so I have to go with the php setting of vanilla and PHP. Any tips and pointers are welcome.

Pay attention to . This does not apply to a multisite, as it deprives the benefits of a common media gallery and users, etc.

+8
php apache wordpress
source share
4 answers

If you want to use one installation of WordPress, you need to either run WordPress Multisite (which you do not want to do) with the plugin for domain matching, or force WordPress to accept any host name (domain) of the request for and serve content conditionally in each domain.

(The following assumes that you are running both example.com and secondary.com , and both of them are pointing to the same server where WordPress is installed. In addition, the following examples were pulled right out of my head without any real tests or verifications, it may or may not work as it is.)

Adopting WP accepts multiple host names for requests

Typically, WordPress is tied to a single name / domain, where it can serve pages and content. The domain is stored in the lines wp_options siteurl and home as http://www.example.com , for example.

You can override this value in wp-config.php with the PHP constants WP_SITEURL and WP_HOME . First you need to make sure that the main domain (for example, maindomain.com) is set correctly in the rows of the wp_options siteurl and home table. Then you need to configure the wp-config.php so that WP accepts any domain as such:

 define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST']); define('WP_HOME', 'http://' . $_SERVER['HTTP_HOST']); 

(Remember to configure HTTP and HTTPS if necessary.

Now your WP installation should open just fine when the domain is pointed to the server. You can have as many domains as you like, and all of them make WP download upon access.

Redirecting a page to another domain

You can use either .htaccess or WP code to redirect. If the redirection is application / site specific and should work anywhere, try using the WP code, which is likely to follow in the source code repositories, etc.

 add_action('template_redirect', function () { $isMainDomain = strpos($_SERVER['REQUEST_URI'], 'example.com') !== false; // We are on primary domain and on domain.com/b if ($isMainDomain && is_page('b')) { wp_redirect('http://secondary.com'); exit; } // You need to validate child pages too and redirect where needed. }); 

Add this to the plugin or functions.php theme. Now, when someone accesses page b in the primary domain, they will be redirected to secondarydomain.com .

To achieve this with .htaccess, you can try the following

 RewriteEngine on RewriteBase / // Redirect page `b` and all child pages to other domain. RewriteCond %{HTTP_HOST} example.com$ RewriteRule ^b/(.*)$ http://www.secondary.com/$1 [L,QSA,R=301] 

Removing page skips from a page in WP

You now have a user sitting at http://www.secondary.com , but the user sees the front of your WP installation instead of the contents of page b .

With rewriting .htaccess, you can proxy these domain requests to a specific "subdirectory" if you need to.

 RewriteEngine on RewriteBase / # Something like the following should hide the `b` page/dir from requests. RewriteCond %{HTTP_HOST} secondary.com$ RewriteRule ^(.*)$ http://www.secondary.com/b/$1 [L,QSA] 

This rule first checks if the request was against the secondary.com domain name. If so, the server rewrites all requests to the WordPress b page and its subpages.

Now that someone opens http://www.secondary.com , he should go to http://www.secondary.com/b and display the page instead.

(Note: I have not tested the rule above so that it may or may not work as is.

Permalink Fixing

You now have a problem with permalinks to page b . Getting permalink returns http://www.example.com/b when it should return http://www.secondary.com .

Adding a filter for permalinks should fix this problem:

 add_filter('post_link', function ($link, $post) { if (is_admin()) { // Don't change in admin, or editing pages may break return $link; } // Check here that if the $post is either page `b` // or a child of that page // then return the proper link instead. return 'http://www.secondary.com/foo/bar'; }); 

This is a shallow approach that does not take into account media or user behavior. You need to confirm that both domains allow cross-start requests and that user session cookies are stored properly for cross-start sessions where necessary.

What I would like to offer is a study of how to connect to the media download process so that the two WP installations use the same directory and data. The same directory is accessible with the upload_dir filter, and attachments are saved similarly to messages, so you can duplicate uploads to two sites using wp_insert_post or the like.

In addition, you can use two WP installations with one user table, using some wp-config.php tricks.

+8
source share

SO, basically: when a user navigates to a specific part of the site - say, a.com/b, the URL should be changed to b.com.

It looks like you just need to create a file / controller / post located at a.com/b that will redirect the browser to b.com .

The easiest way is to create a directory with the name b in the root directory and place the default apache file there (index.htm, index.php, etc.), and this file will have a redirect header.

An ideal way to do this is to transfer 301 redirects using .htaccess . Something like

 Redirect 301 /b http://b.com 

or with a wildcard:

 RedirectMatch 301 /b(.*) http://b.com/$1 

(there are many htaccess resources on the Internet)

These are two extremes, and there are many other options between them, you should choose the one that works best for you, based on how many redirects exist, if there are any patterns, if you want to generate them dynamically, etc.


Problems that you may have here (you never described the problem completely, so I’m just saying), in case you want to return to a.com, and you can’t set up a similar redirect to b.com, In this case I suggest you just say on a.com/b, having b.com rendered inside the iframe.

+5
source share

If you do not want to mess with wp filters or htaccess rules

You can simply create index.html inside a directory called / b / or create a page with slug with the value 'b' a.com/b with this code in plain html

 <script>top.location='http://b.com'</script> 
0
source share

Just add a note © at the bottom of b.com, making it part of a.com (or just add “This site is part of a.com” or something like that). If you want to link a.com with b.com, add a link, for example, <a href = "b.com"> b.com </a>
If you want to get the domain name inside a.com, just redirect from a.com/b to b.com ...

0
source share

All Articles