There are two web sites, lets call them and; they've been completely separate up until now. However, there are "synergies", so the client wants to make part of (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, the URL should change to

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.


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 and 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 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. 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'], '') !== false;

    // We are on primary domain and on
    if ($isMainDomain && is_page('b')) {

    // 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

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}$
RewriteRule ^b/(.*)$$1 [L,QSA,R=301]

Removing the page slug from a page in WP

Now you have a user sitting at, 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}$
RewriteRule ^(.*)$$1 [L,QSA]

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

Now when someone opens it should map to 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, when it should return 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 '';

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.

  • 9
Reply Report

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

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

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

or with a wildcard:

RedirectMatch 301 /b(.*)$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 and you can't set up the similar redirection on In that case I suggest you simply say on while having rendering inside an iframe.

  • 5
Reply Report

If you don't want to mess with wp filters or your htaccess rules

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

  • 0
Reply Report

Just add a © notice to the bottom of making it part of (or just add "This site is part of" or something like that). If you want to link to then add a link like
<a href=""></a>
. In case you want a domain name inside then just do a redirection from to

  • 0
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions