Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different domain for section of website

There are two web sites, lets call them a.com and b.com; they've been completely separate up until now. However, there are "synergies", so the client wants to make b.com part of a.com (same media library, users, host, database et cetera)... but with it's own domain and design. A site within the site, with its own domain.

SO, basically: when a user goes to a specific part of the site - let's say a.com/b, the URL should change to b.com.

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

But I'm using Wordpress (already made the main site), so I should go with vanilla PHP and Apache tweaking. Any tips and pointers are welcome.

Please note: This is not about multisite, as that would void the benefits of a common media gallery and users etc.

like image 973
Olemak Avatar asked Feb 01 '16 08:02

Olemak


People also ask

Can you have 2 domains for one website?

Many names, one destination With most registrars, it's easy to forward multiple domains to your website so you can simply create one site and then redirect visitors who type one of your other domain names to that one website.


2 Answers

If you want to use a single WordPress installation, you either want to run WordPress Multisite (which you do not want to do it seems) with a domain mapping plugin, or then make WordPress accept any request hostname (domain) to operate on and serve content conditionally on each domain.

(The following assumes you control both example.com and secondary.com and they are both pointed to the same server where the WordPress installation lives. Also the following examples have been pulled straight out of my head with no real testing or validation involved, it may or may not work as is.)

Making WP accept multiple hostnames for requests

Normally WordPress is tied to a single hostname/domain where it can serve pages and content. The domain is stored into the wp_options siteurl and home rows as http://www.example.com for example.

You can override this in wp-config.php by using the WP_SITEURL and WP_HOME PHP constants. First you need to make sure the main domain (e.g. maindomain.com) is set properly to the wp_options table rows siteurl and home. Then you need to adjust the wp-config.php file to make WP accept any domain as such:

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

(Make sure to adjust between HTTP and HTTPS if needed.

Now your WP installation should open just fine when a domain is pointed to the server. You can have as many domains as you wish and they all make WP load when accessed.

Redirecting a page to another domain

You can use either .htaccess or WP code to do redirects. If the redirect is application/site specific and should work anywhere, try to use WP code as that is more likely to follow around in source code repos and such.

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 that to a plugin or a theme's functions.php. Now when anyone is accessing the page b on the main domain, they will be redirected to secondarydomain.com.

To achieve this with a .htaccess you could 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 the page slug from a page in WP

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

With .htaccess rewrites, you can proxy those domain requests to a certain "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]

That rule first checks whether the request was against the secondary.com domain name. If it was, the server rewrites all requests to the WordPress page b and it's subpages.

Now when someone opens http://www.secondary.com it should map to http://www.secondary.com/b and show the page instead.

(Note: I did not test the above rule so it may or may not work as is.

Fixing permalinks

Now you have a problem with permalinks regarding the page b. Getting the permalink returns http://www.example.com/b, when it should return http://www.secondary.com instead.

Adding a filter for permalinks should fix that issue:

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 which does not consider how media or users behave. You need to validate that both domains allow cross-origin requests and that user session cookies are saved properly for cross-origin sessions where needed.

What I would also suggest is researching how to hook into the media upload process to make two WP installations use the same directory and data. The same directory is achiveable with the upload_dir filter and the attachments are saved similarly to posts so you can duplicate uploads to two sites using wp_insert_post or similar I presume.

Additionally you can use two WP installations with a single users table using some wp-config.php trickery.

like image 80
ojrask Avatar answered Oct 16 '22 14:10

ojrask


SO, basically: when a user goes to a specific part of the site - let's say a.com/b, the URL should change to b.com.

Sounds like you only need to make a file/controller/post located at a.com/b which will redirect the browser to b.com.

The simplest-simplest way is to make a directory named b in the root, and put the apache default file in there (index.htm, index.php, etc), and that file would have redirection header.

The ideal way to do it is to put a 301 redirection using .htaccess. Something like

Redirect 301 /b http://b.com

or with a wildcard:

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

(there are tons of htaccess resources online)

Those are the two extremes, and there are many other options in between, you should choose one that works for you the best based on how many redirections there are, are there any patterns, would you like to be able to generate them dynamically, etc.


The problems you might have here (you never described the issue fully, so I'm just saying), is in case you want to return to a.com and you can't set up the similar redirection on b.com. In that case I suggest you simply say on a.com/b while having b.com rendering inside an iframe.

like image 34
Shomz Avatar answered Oct 16 '22 16:10

Shomz