0

I'm not sure the best title for this question so sorry if it was confusing.

Setup

I have a multi-site install that I regularly add new sites to.

They are generally in the format:

  • example.com (default)
  • sub1.example.com
  • sub2.example.com
  • sub3.example.com
  • sub3.com

I also have a wildcard dns and vshost so I don't have to edit them every time I create a new site:

example.com my.ip.add.ress
*.example.com my.ip.add.ress
sub3.com my.ip.add.ress

<virtualhost *:80>
  Name example.com
  alias *.example.com
  alias sub3.com
</virtualhost>

Sites.php has a few entries like:

sites[sub3.com] = 'sub3.example.com'

All of this works great.

The problem.

Most of these sites are not publicly visible. being disabled with robots.txt and permission preventing users from see them. So I could care less about those. However some sites are public. In the example above the example.com and sub3.com are both public facing sites.

When a user visits rando.example.com the multi-site will send them to example.com but the url will appear as whatever they typed in. Also the sub3.com site will be visible as both sub3.com and sub3.example.com.

The question.

How do I, preferably using only Drupal, redirect the non-existent subdomains to the default. Not just show the default but actually do a 301 redirect to the url desired.

My attempts.

To my knowledge I can not solve this with:
- redirect module as it does not let me specify a subdomain or a wildcard. - global redirect does not affect subdomains. - sites.php as it does not redirect but a sites[*.example.com] => example.com would be cool.

Qualifiers

  • I understand that I could fix this in the vhost by removing the wildcard name and just adding a new entry for each sub-domain. I do not want to have to edit my vhost every time their is a new sub-domain.
  • I understand I may be able to solve this with .htaccess files but I do not want to have to edit that file every time I create a new site either. And I don't really want it to be overridden from default. Plus I have my server pull the file only on reboot and I don't want to reboot apache just to add a site.
  • I understand that canonical urls would prevent bots from indexing unused sub-domains but that's not good enough I don't want users using a subdomain that doesn't exist.

My ideal solution would be a redirect that supports sub-domains or something similar. Changes to sites.php or database changes are the goal.

2

It is called multi-site aliasing, hence the results. Drupal offers you to server multiple domains from the same folder in order to preserve the paths in the database. So you would use the same folder for example.com and sub3.com because you might want to disable one of them later on. Aliasing is meant on a file level here. What you need is a redirect.

A 301 redirect code can be issued by your webserver-software itself (e.g. Apache/nginx config or apache .htaccess) or by sending a header via the scripting software.

The "drupal-way" solution for you would of course be to use a module for sending that header. My recommendation would be multisite_redirect, because it would only need a one time setup for your case outlined above.

  • Looks promising but I don't see wildcard subdomians. *.example.com to redirect to example.com. I cant specify every possible subdomain in my sites.php like the module indicates. – danielson317 May 27 '16 at 16:04
  • Have you looked at the module? There is a greedy rule described towards the bottom of the page? – nhck May 27 '16 at 17:52
  • I did see that. Ill take a closer look when i can but the examples seem to be focused on domains specified in sites.php. – danielson317 May 27 '16 at 17:54
  • 1
    Finally got to sit down and try this out. It works great. I just had to go the the main site and add a redirect from "any" with path * to path *. It only captures the urls that hit the base site so ignores all the children sites. Still need to experiment with the real-site.example.com => real-site.com but I think It seems to be pretty straitforward. Thanks. +1 and Check. – danielson317 Jun 1 '16 at 16:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.