• 5

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 187


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 187
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

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.

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')) {

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

  • 9
Reply Report

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.

  • 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' a.com/b with this code in plain html

  • 0
Reply Report

Just add a © notice to 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 to b.com then add a link like
<a href="b.com">b.com</a>
. In case you want a domain name inside a.com then just do a redirection from a.com/b to b.com...

  • 0
Reply Report