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
Comment #1
realityloop commentedThe 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...
Comment #2
anarcat commentedWell, 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:
Heck, it could even be as simple as:
... 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.
Comment #3
acWe 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
Comment #4
anarcat commentedWhile 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? :)
Comment #5
anarcat commentedSo this is likely to slip past 0.3 (since we're in feature freeze), but here we go.
Comment #6
acHad issues with the site verification after applying this patch
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
Comment #7
anarcat commentedAre 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.
Comment #8
acok.. 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
Comment #9
realityloop commentedPatches 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
Comment #10
anarcat commentedHum, 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?).
Comment #11
anarcat commentedI 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...
Comment #12
realityloop commentedsomething getting added to the vhost entry now, but the redirection isn't working.
Created vhost entry:
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)
Comment #13
anarcat commentedI 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..
Comment #14
realityloop commentedI'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.
Comment #15
steveparks commentedI've tried this manual method (as per realityloops suggestion) on my sites, and it works well.
Comment #16
anarcat commentedI *think* i fixed this in git, I have updated the redirection branch in git. Here is a reroll of the patches. Please test
Comment #17
Anonymous (not verified) commentedPatches 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:
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 :)
Comment #18
anarcat commentedThis 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...
Comment #19
Anonymous (not verified) commentedI 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
Comment #20
anarcat commentedCommitted. We'll see how it works.
Comment #22
greenmachine commentedI 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)
Comment #23
greenmachine commentedHere is the code for provision_apache_vhost_redirect.tpl.php
Comment #24
Anonymous (not verified) commentedYou 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
Comment #25
Anonymous (not verified) commentedClosing this, we won't be making retrospective changes to 0.3 anyway, so the only real option is to upgrade.