I apologize if this is a duplicate. I've been searching through the issue queue and my head is spinning but I haven't quite figured this out.

I have legacy nodes from before DA that I am filing into the proper subdomain. Each of these are assigned to the subdomain as well as the main domain (the main domain acts as a hub for all content from all subdomains). Because they are assigned to the main domain, you can go to these nodes on the main domain with no error. This isn't what I want, though. I want them to be listable on the main domain but actually go to the subdomain when clicked.

I turned on the rewrite option so all lists point you to the domain and that's great. But the search engines and other links are already pointing to the main domain for these nodes. I can't use path redirect because the path I'm redirecting from is a valid path and it errors. I saw the domain redirect module in one of these issues but that seems to be for redirecting from content that isn't available on the current domain, which isn't the case.

Since there's only about 200 legacy nodes and future nodes should be ok with the rewrite rule, I wonder if I should put something in .htaccess? Is there a better way that I'm missing?

Thanks,

Michelle

CommentFileSizeAuthor
#15 domain-714986-D6.patch1.17 KBmikeytown2
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

agentrickard’s picture

Domain redirect is the closest thing to what you describe. DA only handles the links that Drupal writes, it doesn't control inbound requests.

Michelle’s picture

Status: Active » Fixed

Yeah, and domain redirect doesn't do what I want. If there's nothing in DA for this, I'll investigate adding the links manually to .htaccess.

Thanks,

Michelle

agentrickard’s picture

It would be pretty trivial to write a small module that handles this. You might need a redirect flag on the node, though.

Otherwise, a little code in hook_init() might do it. You could also do this in hook_nodeapi('view').

function custom_init() {
  global $_domain;
  $args = array();
  for ($i = 0; $i < 2; $i++) {
    $args[$i] = arg($i);
  }
  if ($args[0] == 'node' && is_numeric($args[1]) && empty($args[2])) {
    $domain = domain_get_node_match($args[1]);
    if ($domain['domain_id'] != $_domain['domain_id']) {
     domain_goto($domain);
   }
}

function hook_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  global $_domain;
  // Only act when viewing a full node.
  if ($op != 'view' || !empty($a3)) {
    return;
  }
  $domain = domain_get_node_match($node->nid);
  if ($domain['domain_id'] != $_domain['domain_id']) {
   domain_goto($domain);
  }
}

The second code is probably cleaner, since it doesn't depend on module weight for execution order. You'd also want to check that $domain is active and accessible to the user.

agentrickard’s picture

Category: support » feature
Status: Fixed » Needs review

You know, if that second piece of code works, I would consider making it a configurable option in the module.

Michelle’s picture

Wow, thanks! I'll give it a whirl this evening when the kids are in bed and let you know. Would be great having it right in the module. I'm sure I'm not the only one to ever have legacy nodes, though my having them on both the subdomain and main domain might be a little edge case-y.

Michelle

agentrickard’s picture

It has come up more than once before, and I have rejected it. But recent changes (and the domain_get_node_match() function make this really simple to do, I think.

But that assumes Drupal makes it to nodeapi $op 'view'. It might not.

Michelle’s picture

I got caught up with other stuff tonight and just realized I forgot to try this now as I'm headed to bed. Tomorrow night is spoken for, I'm afraid, but will try it out first thing Wednesday evening. I appreciate you helping out and sorry I forgot to test it out.

Michelle

agentrickard’s picture

Issue tags: +favorite

Tagging.

nonsie’s picture

What about use cases where such redirect is not desired e.g. if a node is published to the default domain and a subdomain and the user is viewing it on the default domain and should not be redirected? In a way this is similar to how we handle SEO rewrite at the moment - either show on all domains or redirect to the best match.

If this is going in it needs to be configurable per install.

Just my 2 cents.

agentrickard’s picture

Yes, config per install was my thought, too. Config per node would be even better, but not quick.

agentrickard’s picture

Version: 6.x-2.x-dev » 7.x-2.x-dev
hefox’s picture

(Been using the hook_init without any troubles on my own site for a while, needed it for another site so stuffed it into a module. Didn't try the nodeapi, as I use panels. Tell me if ya prefer I don't have it up on github. http://github.com/hefox/domain_redirect.)

agentrickard’s picture

There's easier code for this:

function custom_init() {
  global $_domain;
  if ($node = menu_get_object())) {
    $domain = domain_get_node_match($node->nid);
    if ($domain['domain_id'] != $_domain['domain_id']) {
     domain_goto($domain);
    }
  }
}
mikeytown2’s picture

subscribe/tracking

mikeytown2’s picture

FileSize
1.17 KB

Here's a patch for 6.x. Gives user link to story on source domain in a message box. This patch makes the assumption that you do not want to go to domain_id = 0 via #867934: Do not redirect to domain_id=0

Sidenode: I think calling something like domain_access_denied() which would then allow for some default behavior and hooking when access_denied is called could be useful.

agentrickard’s picture

New features go to 7.x.

agentrickard’s picture

Version: 7.x-2.x-dev » 7.x-3.x-dev

7.x.3 to be exact.

agentrickard’s picture

Status: Needs review » Needs work
Michelle’s picture

I forgot about this issue. Just as an FYI, I am no longer using DA. Nothing against the module; just turned out to be too confusing for my users and I'm scaling down the site, anyway. I think it's still a decent FR, but don't do it just for me. :)

Michelle

rogical’s picture

+1 www.example.com/article/news1 --> news.example.com/artile/news1 this is very much need!

camdarley’s picture

I think that:

$domain = domain_get_node_match($node->nid);
    if ($domain['domain_id'] != $_domain['domain_id']) {

could be replaced by

$domains = domain_get_node_domains($node->nid);
    if (!in_array($_domain['domain_id'], $domains['domain_id'])) {

It works for me and allow to publish content on multiple domains.

jan.s’s picture

Hi camdarley,

keep in mind you must provide a new domain to domain_goto().

See my solution at this issue: http://drupal.org/node/1566058

rogical’s picture

Title: Redirect to domain source? » Redirect to 'Source domain' with 'Send to all affiliates' enabled

I think this feature is very common, big sites like Yahoo!, promte/sticky contents from subdomain show on main domain site, and redirect on visit.

#1646586: Auto create domain url based on Source domain is also a good substitute.