Adding the following block of code above the vhost entry for a site will work similarly to the current aliasing options, with the added advantage of changing the browser url from domain.com to www.domain.com for any url accessed.

I think this would be a good additional option

Added above Aegirs default vhost entry:

<VirtualHost *:80>
  ServerName domain.com
  RedirectMatch permanent ^(.*) http://www.domain.com$1
</VirtualHost>

Default created by Aegir:

<VirtualHost *:80>
  DocumentRoot /var/aegir/drupal-6.13/

  ServerName www.domain.com

# Extra configuration from modules:

    # Error handler for Drupal > 4.6.7
    <Directory "/var/aegir/drupal-6.13//sites/www.domain.com/files">
      SetHandler This_is_a_Drupal_security_line_do_not_remove
    </Directory>

</VirtualHost>

Comments

realityloop’s picture

The advantage of this is in reducing content duplication for sites that shouldn't be serving to multiple domains.

More information can be found at: http://blog.melimato.com/using-apaches-redirectmatch-instead-of-serveral...

anarcat’s picture

Title: vhost addition option for domain aliasing » domain redirection

Well, this is really a different feature: domain redirection. Aliases may be useful and desired in certain conditions (think of the domain module for example) so I wouldn't want to ditch that functionality.

But I agree that what we really want, by default, is redirection.

I'm not sure creating a separate vhost is necessary, however. I am thinking of something amongst those lines:

<VirtualHost *:80>
  ServerName www.example.com
  ServerAlias example.com
  ServerAlias other.example.com
  RewriteCond ${HTTP_HOST} example.com [OR]
  RewriteCond ${HTTP_HOST} other.example.com [NC]
  RewriteRule ^/*(.*)$ http://www.example.com/$1 [L,R=301]
</VirtualHost>

Heck, it could even be as simple as:

<VirtualHost *:80>
  ServerName www.example.com
  ServerAlias example.com
  ServerAlias other.example.com
  RewriteCond ${HTTP_HOST} !^www.example.com$ [NC]
  RewriteRule ^/*(.*)$ http://www.example.com/$1 [L,R=301]
</VirtualHost>

... ie. don't need to list every domain, just use a "not" (!).

That would be fairly simple to implement: a frontend module that would just add a checkbox to enable redirection and a backend module that would implement that hook I always forget the name to add stuff to the apache vhost.

Actually, this could be strapped on the alias module...

I support this feature.

ac’s picture

We are looking at expanding the hosting_alias module to do this... ie if you have a site foo.example.com and you want to create the alias bar.com, with all traffic to foo.example.com redirected to bar.com. Any suggestions for the best way to implement this appreciated. I am in favour of moving the alias field out of the node form and into its own tab a la migrate etc

anarcat’s picture

Assigned: Unassigned » anarcat

While re-reading the code to answer that last question, I figured that there *was* a conflict with the alias system... Currently, aliases include adding ServerAlias directives to the vhost (hardcoded in web_server/provision_apache_vhost.tpl.php, which are fed by the alias module in the frontend. That's fine *but* the backend also creates *symlinks* in sites/ for all the aliases. If we implement redirection, that will have to be turned off.

So what I think is this: the frontend alias module should have an extra "redirect" flag that turns the aliases into redirection (that would be in hosting_alias_form_alter()). That flag then would need to be passed to the backend (in hosting_alias.drush.inc). Then the backend needs to use that...

I'm just going to write up some patch for you to test, how is that? :)

anarcat’s picture

Assigned: anarcat » Unassigned
Status: Active » Needs review
StatusFileSize
new2.59 KB
new5.77 KB

So this is likely to slip past 0.3 (since we're in feature freeze), but here we go.

ac’s picture

Had issues with the site verification after applying this patch

Could not change permissions sites/lolcows.pagebuild.net to 493 (chmod to 755 failed on sites/lolcows.pagebuild.net)
Could not change permissions sites/lolcows.pagebuild.net/files to 1528 (chmod to 2770 failed on sites/lolcows.pagebuild.net/files)

Also the patch implies (didn't get it working so can't be sure) that the alias will redirect to the master url. We are looking to do the reverse so an option to do that would be cool. Becomes problematic with more than one alias

anarcat’s picture

Are you sure those issues are related with the patch? I think the problem you are describing is common in rc2, and is resolved by #529382: proper recursive functions for chmod/chgrp/chown and #203204: Uploaded files have the permissions set to 600.

ac’s picture

ok.. I didn't have the file perms patch applied but I was using head of aegir so that is probably my issue. I will try again with the core patch applied

realityloop’s picture

Status: Needs review » Needs work

Patches applied cleanly.

Site installed, but no alias added to host file

Hosting Settings as follows:
Site Aliases
( empty ) Domain used for automatic subdomain hosting
(uncheck) Generate www.domain.com alias automatically
(uncheck) Generate domain.com alias automatically
(checked) Use redirects instead of aliases by default

Create Site settings:
Domain name: domain.com
Domain aliases: www.domain.com
Redirection: (checked)

vhost file created

<VirtualHost *:80>
  DocumentRoot /var/aegir/drupal-6.13/

  ServerName domain.com

# Extra configuration from modules:

    # Error handler for Drupal > 4.6.7
    <Directory "/var/aegir/drupal-6.13//sites/domain.com/files">
      SetHandler This_is_a_Drupal_security_line_do_not_remove
    </Directory>

</VirtualHost>
anarcat’s picture

Status: Needs work » Needs review

Hum, that's odd: I can reproduce your problem. It seems that automatic aliases work but manual aliases don't.

I reverted the patch and manual aliases did work, so I did something that broke manual aliases on the frontend (or the backend?).

anarcat’s picture

StatusFileSize
new5.73 KB

I didn't mean to change the status here originally, but anyways, here's a reroll of the patchset, testing required again. Only the hosting part changed, so I'm just uploading this patch to avoid confusion, the provision patch is still valid.

This one was really hard to figure out, i was using db_result of db_fetch_array and got confused over that...

realityloop’s picture

something getting added to the vhost entry now, but the redirection isn't working.

Created vhost entry:

<VirtualHost *:80>
  DocumentRoot /var/aegir/drupal-6.13/

  ServerName domain.com
  ServerAlias www.domain.com

  RewriteCond ${HTTP_HOST} !^domain.com$ [NC]
  RewriteRule ^/*(.*)$ http://domain.com/$1 [L,R=301]

# Extra configuration from modules:

    # Error handler for Drupal > 4.6.7
    <Directory "/var/aegir/drupal-6.13//sites/domain.com/files">
      SetHandler This_is_a_Drupal_security_line_do_not_remove
    </Directory>

</VirtualHost>

My earlier example code works without needing the multiline rewrite rule, see below for a working example:(eg: this will automatically redirect http://domain.com/contact to http://www.domain.com/contact)

<VirtualHost *:80>
  ServerName domain.com
  RedirectMatch permanent ^(.*) http://www.domain.com$1
</VirtualHost>

<VirtualHost *:80>
  DocumentRoot /var/aegir/drupal-6.13/

  ServerName www.domain.com
# Extra configuration from modules:

    # Error handler for Drupal > 4.6.7
    <Directory "/var/aegir/drupal-6.13//sites/www.domain.com/files">
      SetHandler This_is_a_Drupal_security_line_do_not_remove
    </Directory>

</VirtualHost>
anarcat’s picture

Status: Needs review » Needs work

I like your approach better. What's also interesting is that having a separate vhost clarifies things... Furthermore, this should become the basis of the vhost engine: right now, all this is tightly coupled with the sites engine, while we may want to allow users to create arbitrary redirections and things like that. The SSL engine would use things like that too. So I support the idea of a separate vhost (and a separate file?).

See #541754: abstract away vhost engine for the vhost engine abstraction..

realityloop’s picture

I've tended to put them in the same files as per my example, because I see them as related..

But it would make logical sense to split them in case you later decide to set up a separate site.

Having them separate would make it a lot easier if you did decide to do that.

steveparks’s picture

I've tried this manual method (as per realityloops suggestion) on my sites, and it works well.

anarcat’s picture

Version: 6.x-0.3-rc2 » 6.x-0.3
Status: Needs work » Needs review
StatusFileSize
new5.52 KB
new5.78 KB

I *think* i fixed this in git, I have updated the redirection branch in git. Here is a reroll of the patches. Please test

Anonymous’s picture

Patches applied cleanly, I had one issue though:

I had set "Use redirects instead of aliases by default", but the redirect wasn't working. This turned out to be because, although it had appended to the site's vhost config correctly, the main VirtualHost above it still added a 'ServerAlias' parameter of the url to be redirected, which meant the new added VirtualHost tag was ignored. Removing the ServerAlias line and restarting Apache of course made the redirect work immediately

So my vhost config looked like this:

<VirtualHost *:80>
  DocumentRoot /var/aegir/platforms/drupal-6.13

  ServerName www.535098.com
  ServerAlias 535098.com


# Extra configuration from modules:

    # Error handler for Drupal > 4.6.7
    <Directory "/var/aegir/platforms/drupal-6.13/sites/www.535098.com/files">
      SetHandler This_is_a_Drupal_security_line_do_not_remove
    </Directory>

</VirtualHost>
<VirtualHost *:80>
        DocumentRoot /var/aegir/platforms/drupal-6.13

    ServerName 535098.com

    RedirectMatch permanent ^(.*) http://www.535098.com$1
</VirtualHost>

removing that ServerAlias fixes it. Also I don't think the DocumentRoot is required for the redirect VirtualHost, as it never needs to read from a dir.

Patch attached fixes the above (doesn't print a ServerAlias if $redirection), removes DocumentRoot from the redirection template. Also tiny typo in the Site Aliases form and a slight reword -- feel free to ignore this, it's just opinion :)

anarcat’s picture

Status: Needs review » Needs work

This will need to be ported now that the ports stuff hit the tree (fairly simple: we need to use the $site_port in the template instead of just "80"), see http://cvs.drupal.org/viewvc.py/drupal/contributions/modules/provision/w...

Anonymous’s picture

Status: Needs work » Needs review
StatusFileSize
new6.44 KB
new5.67 KB

I don't know what went wrong with my other patches the other night but I blame the cool early morning air.

New patches attached, and I take into your #18 one extra $site_port in provision_apache_vhost_disabled.tpl.php

anarcat’s picture

Status: Needs review » Fixed

Committed. We'll see how it works.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

greenmachine’s picture

Status: Closed (fixed) » Needs review

I might be missing something, but I am re-opening this issue because the patches in #19 do not seem to be a complete solution. Specifically, the web_server/provision_apache_vhost_redirect.tpl.php file created by the http://drupal.org/files/issues/535098_redirection_provision_mig5.patch patch fails to handle cases where multiple aliases are entered by the user in the aegir site node edit form.

With the code in that patch, it appears to me that by using array_pop() on the $aliases array, only one redirect will be created (for the last alias entered into the field by the user). Other aliases are ignored and thus will not work.

Again, I could be missing something, because I applied these patches to aegir 0.3 (I am not using the 0.4 alpha).

Here is my code to fix the issue, using a foreach() around the template code so that a entry is created for each alias entered for the site (instead of just he last one). The only change is in provision_apache_vhost_direct.tpl.php which should be changed to:

(oops - posting as code got messed up due to the http:// stuff, will attach a file)

greenmachine’s picture

StatusFileSize
new320 bytes

Here is the code for provision_apache_vhost_redirect.tpl.php

Anonymous’s picture

You might want to look at how it's done in 0.4. There won't be any changes made to the 0.3 release as it is no longer really being maintained.

From vague memory (this is a really old ticket), that was indeed not the correct way to do it, and it got fixed in 0.4:

Take a look at http://git.aegirproject.org/?p=provision.git;a=commitdiff;h=85f8249ba056...

but it may not be the same issue.

In any case I highly recommend you upgrade to the latest 0.4 alpha (yes despite alpha) - if the problem still exists there for you, that'll likely get someone's attention more to see it fixed

Anonymous’s picture

Status: Needs review » Closed (fixed)

Closing this, we won't be making retrospective changes to 0.3 anyway, so the only real option is to upgrade.