domain redirection

realityloop - July 30, 2009 - 00:00
Project:Hosting
Version:6.x-0.3
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:closed
Description

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>

#1

realityloop - July 30, 2009 - 00:03

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

#2

anarcat - July 30, 2009 - 01:07
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.

#3

ac - July 30, 2009 - 08:27

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

#4

anarcat - July 30, 2009 - 17:30
Assigned to:Anonymous» 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? :)

#5

anarcat - July 30, 2009 - 18:28
Assigned to:anarcat» Anonymous
Status:active» needs review

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

AttachmentSize
535098_redirection_provision.patch 2.59 KB
535098_redirection_hosting.patch 5.77 KB

#6

ac - July 31, 2009 - 07:37

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

#7

anarcat - August 1, 2009 - 05:34

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.

#8

ac - August 1, 2009 - 06:55

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

#9

realityloop - August 5, 2009 - 01:06
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>

#10

anarcat - August 5, 2009 - 01:39
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?).

#11

anarcat - August 5, 2009 - 03:32

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

AttachmentSize
535098_redirection_hosting.patch 5.73 KB

#12

realityloop - August 6, 2009 - 00:58

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>

#13

anarcat - August 6, 2009 - 13:10
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..

#14

realityloop - August 7, 2009 - 02:31

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.

#15

pilot - September 1, 2009 - 13:34

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

#16

anarcat - September 3, 2009 - 09:59
Version:6.x-0.3-rc2» 6.x-0.3
Status:needs work» needs review

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

AttachmentSize
535098_redirection_provision.patch 5.78 KB
535098_redirection_hosting.patch 5.52 KB

#17

mig5 - September 4, 2009 - 13:56

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 :)

AttachmentSize
535098_redirection_hosting_comment17.patch 4.62 KB
535098_redirection_provision_comment17.patch 5.72 KB

#18

anarcat - September 7, 2009 - 13:19
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...

#19

mig5 - September 7, 2009 - 13:57
Status:needs work» needs review

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

AttachmentSize
535098_redirection_hosting_mig5.patch 5.67 KB
535098_redirection_provision_mig5.patch 6.44 KB

#20

anarcat - September 7, 2009 - 14:12
Status:needs review» fixed

Committed. We'll see how it works.

#21

System Message - September 21, 2009 - 14:20
Status:fixed» closed

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

 
 

Drupal is a registered trademark of Dries Buytaert.