Web Hosting Forum | Lunarpages


*
Welcome, Guest. Please login or register.
Did you miss your activation email?



Login with username, password and session length
March 16, 2010, 09:26:23 AM

Pages: [1]   Go Down
  Print  
Author Topic: URI Rewrite Subdomains to Single Directory  (Read 1005 times)
emanaton
Trekkie
**
Offline Offline

Posts: 13


« on: August 13, 2007, 09:28:06 AM »

Greetings All,

I don't know that this is a "Basic" question, but this forum seems to be the most appropriate place to post this inquiry. I've read through every related post in the forum that I could find and cannot locate any answer to this question; if this question sounds familiar to you and you are SURE it's been answered elsewhere then please do let me know that URL!

I need to have several subdomains setup such that they all resolve to the same directory on the server, but must not use a “30n Redirect” to achieve this; I must preserve the subdomain URL as the user typed it. As you know, each subdomain you create on LP results in a directory to which which the URL will resolve, but I need each subdomain to resolve to a SAME directory.

For example, if I create sub_a.mydomain.com and sub_b.mydomain.com, then "sub_a" and "sub_b" directories are created. What I need is for the files to be served out of sub_x whenever a user goes to one of these subdirectories. I don't care of the subdirectory persists and remains empty, I just need for it to "invisibly" retrieve all files served from the other directory.

I'm fairly fluent with ModRewrite and have made several attempts at resolving the issue, but I think the problem here must be that LP is rewriting the URI in a way I cannot fathom (and therefore cannot correct). I am sure that LP must have a way to resolve this issue, as use of subdomains in this fashion is a fairly standard issue across the industry. I can probably figure this out if I start hacking at my LP account, but that seems like a drastic and rude thing to do to LP, so I'm hoping a more reasonable answer is available.

Ideally, I would love to setup a standard “catchall” and deal with subdomain redirection myself vis a vis .htaccess directives, but I get the feeling lunar pages does not allow this for some arcane reason?

Thanks in advance for you help!

Sean P. O. MacCath-Moran
www.triskelemedia.com
« Last Edit: August 15, 2007, 06:56:04 PM by emanaton » Logged
emanaton
Trekkie
**
Offline Offline

Posts: 13


« Reply #1 on: August 15, 2007, 03:28:36 PM »

So - don't you just love it when someone answers their own question?  Hug If so, let the love poor in!   Luff Ya

WARNING
The process described below contains dangerous magic foo used by the high arcane wizards of the internet, and much like Mickey Mouse, misuse of such high-foo can cause you all kinds of grief. If you use the tequniques described below and it melts your server or your mother disowns you or WHATEVER then that is because you did it wrong and it's ALL YOUR FAULT ; i.e. I take no responsibility for the use or misuse of the code and directions herein.

Recap
In situations such as a shared Drupal installation, it is difficult on LunarPages to create a subdomain that points to the same set of files (e.g. www.pridomain.tld and subdomain.pridomain.tld both pointing to /drupal/ on the server). When you create a subdomain vis a vis C-Panel then a new directory is created and the subdomain's root is set to that subdirectory. The LunarPages prescribed fix for this is to delete the folder and recreate it as a symlink to the desired forder; creating the subdomain aaa.domain.com in C-Panel creates a directory called aaa, but you want requests to aaa.domain.com to go to the /xxx/ directory, so delete /aaa/ and then create a symlink to /xxx/ called /aaa/. The problem is that there is no explicit way to create a symlink folder, so it must be done via a server side script. The helpful folks at the Lunar Pages help-desk aided me in coming a viable solution.

Process
  • From the LunarPages C-Panel, log in and create a subdomain.
  • Using either FTP or the LunarPages File Manager in C-Panel, log in and delete the new subdomain directory.
  • Make a call to the PHP Script below, passing in the following arguments (replace the caps values):
  • magicpasskey=PASSWORD SET IN SCRIPT
  • symlinktarget=TARGET-NAME
  • symlinklink=SUBDOMAIN-NAME
[li]e.g. To create a symlink called "fubar" that points to the "snafu" directory with a password of "LunarPagesROCKS" after having saved the script below to your domain as "makesymlink.php", call www.yourdomain.com/makesymlink.php?magicpasskey=LunarPagesROCKS&symlinktarget=snafu&symlinklink=fubar[/li]
[/list]

PHP Script
Code:
<?php
  
// I could have have derived the root file name, but decided not to bother with that level of sophistication...
  
$LPUserName 'YOUR LUNAR PAGES LOGIN NAME (see $root declaration below)';
  
$myPassKey 'YOUR SECRET AND SECURE PASSWORD';
  
$root '/home/'.$LPUserName.'/public_html/';
  
$target = (array_key_exists('symlinktarget',  $_REQUEST) ? $root.$_REQUEST['symlinktarget'] : false);
  
$link = (array_key_exists('symlinklink',  $_REQUEST) ? $root.$_REQUEST['symlinklink'] : false);
  
$magicpasskey = (array_key_exists('magicpasskey',  $_REQUEST) ? ($_REQUEST['magicpasskey'] == $myPassKey true false) : false);
  if (
$link === false || $target === false || $magicpasskey === false){
    echo 
'<div>WHO GOES THERE, WHAAAT?!</div>';
  } else {
    
$target_is_dir is_dir($target);
    
$target_is_link is_link($target);
    
$link_is_dir is_dir($link);
    
$link_is_link is_link($link);
    echo 
'<div>target_is_dir: '.($target_is_dir 'true' 'false').'</div>';
    echo 
'<div>target_is_link: '.($target_is_link 'true' 'false').'</div>';
    echo 
'<div>link_is_dir: '.($link_is_dir 'true' 'false').'</div>';
    echo 
'<div>link_is_link: '.($link_is_link 'true' 'false').'</div>';
    if (
$link_is_dir || $link_is_link) {
      echo 
'<div>Cannot proceed while directory exists. I mean, YOU might be up to all kinds of no good, eh?</div>';
    } else if (
$target_is_link) {
      
//just doing this as a protective measure - you COULD link to a 
      //link, but in this context that is not what we are going for
      
echo '<div>Cannot link to a link - what are you trying to play at here?!</div>';
    } else {
      if (
symlink($target$link)) {
        echo 
'<div>Success!  Created symlink to <strong>'.$target.'</strong> called <strong>'.$link.'</strong>!!</div>';
      } else {
        echo 
'<div>Hmm... Somethings gone wrong... It did not work!</div>';
      }
    }
    echo 
'<pre>'print_r($_REQUEST); echo '</pre>';
    
phpinfo();
  }

Cron and/or PERL Code
One might build up code to achieve the same effect as this PHP script using PERL OR vis a vis a CRON call instead. In fact, the Lunar Pages techs recommended exactly those solutions to me. What follows is an excerpt of that conversation:

You actually do have the ability to create symlinks under your own account. You can accomplish this either by setting up a cron, having it run once, and then removing it, or you can use the following perl code, editing as needed:

Code:
#!/usr/bin/perl

print "Content-type: text/html\n\n";
my $sym = symlink("THING TO LINK TO","LINK THAT WILL BE CREATED");
print $sym, "(0 means didn't work, 1 means it worked)";

In the above, THING TO LINK TO would be the folder (full paths required) that stores all the files for your subdomains.

LINK THAT WILL BE CREATED would be the name of the directory for each subdomain (ie, /sub__a for sub__a.domain.com). Full paths are also required.

It should also be noted that LINK THAT WILL BE CREATED should not exist. If a folder with that name does exist, you would need to delete it before running the script (true for both the cron approach and perl approach).

Conclusion
I'm currently using the process described with the PHP Script to great effect with Drupal, although I should note that I've relegated several Sub, Parked, and Add On domains to the same folder space. The non-sub-domains were setup by altering the .htaccess file in my domain root as follows:

Code:
Options +FollowSymLinks
RewriteEngine on
IndexIgnore *
Options -ExecCGI

# We always purchase the big three TLDs and relagate them to www.domain.com, sooo:
RewriteCond %{HTTP_HOST} domain1\.net                     [OR,NC]
RewriteCond %{HTTP_HOST} ^domain1\.com$                   [OR,NC]
RewriteCond %{HTTP_HOST} domain1\.org                     [NC]
RewriteRule ^(.*)$ http://www.domain1.com/$1              [R=301,L]

RewriteCond %{HTTP_HOST} domain2\.net                     [OR,NC]
RewriteCond %{HTTP_HOST} ^domain2\.com$                   [OR,NC]
RewriteCond %{HTTP_HOST} domain2\.org                     [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1              [R=301,L]

# We don't like people abusing Lunar Pages bandwidth, so:
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://domain1 .com/.*$      [NC]
RewriteCond %{HTTP_REFERER} !^http://domain1 .com$         [NC]
RewriteCond %{HTTP_REFERER} !^http://domain1 .net/.*$      [NC]
RewriteCond %{HTTP_REFERER} !^http://domain1 .net$         [NC]
RewriteCond %{HTTP_REFERER} !^http://domain1 .org/.*$      [NC]
RewriteCond %{HTTP_REFERER} !^http://domain1 .org$         [NC]
RewriteCond %{HTTP_REFERER} !^http://www.domain1.com/.*$  [NC]
RewriteCond %{HTTP_REFERER} !^http://www.domain1.com$     [NC]
RewriteCond %{HTTP_REFERER} !^http://www.domain1.net/.*$  [NC]
RewriteCond %{HTTP_REFERER} !^http://www.domain1.net$     [NC]
RewriteCond %{HTTP_REFERER} !^http://www.domain1.org/.*$  [NC]
RewriteCond %{HTTP_REFERER} !^http://www.domain1.org$     [NC]
RewriteCond %{HTTP_REFERER} !^http:// domain2  .com/.*$      [NC]
RewriteCond %{HTTP_REFERER} !^http:// domain2  .com$         [NC]
RewriteCond %{HTTP_REFERER} !^http:// domain2  .net/.*$      [NC]
RewriteCond %{HTTP_REFERER} !^http:// domain2  .net$         [NC]
RewriteCond %{HTTP_REFERER} !^http:// domain2  .org/.*$      [NC]
RewriteCond %{HTTP_REFERER} !^http:// domain2  .org$         [NC]
RewriteCond %{HTTP_REFERER} !^http://www.domain2.com/.*$  [NC]
RewriteCond %{HTTP_REFERER} !^http://www.domain2.com$     [NC]
RewriteCond %{HTTP_REFERER} !^http://www.domain2.net/.*$  [NC]
RewriteCond %{HTTP_REFERER} !^http://www.domain2.net$     [NC]
RewriteCond %{HTTP_REFERER} !^http://www.domain2.org/.*$  [NC]
RewriteCond %{HTTP_REFERER} !^http://www.domain2.org$     [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp|zip|pdf|exe|doc)$   - [F,NC]

#Finally, send file requests (where appropriate) to the drupal directory:
RewriteCond %{HTTP_HOST} ^www\.domain2\.com$                [OR,NC]
RewriteCond %{HTTP_HOST} ^www\.domain1\.com$                    [NC]
RewriteRule ^(.*)$ /drupal/$1 [L]

Enjoy!

Sean P. O. MacCath-Moran
www.triskelemedia.com
« Last Edit: August 15, 2007, 06:56:35 PM by emanaton » Logged
codeshark
Newbie
*
Offline Offline

Posts: 1


« Reply #2 on: September 07, 2007, 05:47:05 PM »

Hi,  I am wondering if your solution will work for my needs.  Please review my post below, and if you think your solution will help, let me know so I can dive in and ask some specific questions.  Please note that when I start my drupal development, I want to have my static html site live a public_html before moving the drupal site to public_html, at which time I want to have my test drupal site accessed via a subdomain, and my live drupal site accessed via www.example.com and located in example.com/public_html.  Thanks.

I have been searching the forums here and at lunarpages, and I am in need of some assistance.

I currently have a static html site on lunarpages.  It is accessed by typing www.example.com in the address field of the browser.  The pages are kept at www.example.com/public_html.  I want to keep this site live while I install, test and develop a drupal site at a subdomain called www.tester.example.com.  I then want to move the drupal site to www.example.com/public_html, and have the site accessible by typing www.example.com in the browser address bar.  I then want to be able to test and develop changes to the drupal site at www.tester.example.com, before moving them to the live site at www.example.com/public_html which would be viewed by typing www.example.com, for the home page.  So basically, I want to set up a test site at www.tester.example.com that will allow me to test changes before they are seen by the public at www.example.com.

There are some other factors.  I want to have my drupal file system download method be private, and I want to have my files folder and my images folder one level above the public_html folder for the live site.  I have done this on test drupal installations, both installing into a subdomain and installing into a test directory like www.example.com/test by setting my download method as private, and designating my files folder location as ../files in the files folder path field on the administer>>site configuration>>files system page, and placing the files folder on level above the folder that I install drupal in.  So for my subdomain install the folder I install into is www.example.com/public_html/tester meaning the location of the drupal index.php file is www.example.com/public_html/tester/index.php, which is access via typing www.tester.example.com into the browser, and the location of the files folder is www.example.com/public_html/files.  The location of the images folder, which holds all images including logo etc.. is, you guessed it, example.com/public_html/images.

When I move the drupal files from example.com/public_html/tester to example.com/public_html, making sure that the images and files folders are one level above public_html, the drupal site functions except that it can't find the files folder or the images folder, which are one level above public_html, just as they were one level above example.com/public_html/tester.  This despite the fact that the files folder location is specified as ../files, and image links are specified as ../images.  If I put the images and files folder in example.com/public_html then drupal sees the folders and all links work, but I want the folders to be private and one level above public_html when the site is live to the public in public_html.  I have tried rewriting the $base_url var in settings.php to www.example.com after I move the drupal site, which keeps the site working but the images and files folder links remain broken.  I have also tried rewriting $base_url to www.example.com/public_html, but that totally breaks the site.  If I leave the $base_url commented out it is the same as rewriting it to www.example.com.

My understanding is that using a subdomain such a tester.example.com is the best way to go to avoid broken image links when I move the test site live, and that somehow I have to create a url rewrite using a method such as described here http://www.lunarforums.com/lunarpages_beginner_assistance/uri_rewrite_subdomains_to_single_directory-t42641.0.html , but I am not certain that this method will work, and/or exactly how to do it.

So, I am going a little crazy here.  I want to set up a test environment from which I can move my drupal installation to a live environment, located at www.example.com/public_html, seamlessly without having to edit image links.  I want my files folder and my images folder to be located one level above public_html when the site is live for security reasons.  How is this best accomplished, and what is neccesary.  Do I need to create a symlink for the subdomain?  Should I test and develop in a sub-folder like www.example.com/public_html/tester?  (probably not that, but let me know if you think it would work and how).  Is anyone testing on a subdomain and pushing live seamlessly?  How do you do it?  Are there some rewrite rules I could use via .htaccess? 

I have found similar threads at http://drupal.org/node/169529 and http://www.lunarforums.com/lunarpages_beginner_assistance/uri_rewrite_subdomains_to_single_directory-t42641.0.html, but I am ultimately not understanding the guidance provided there.  Any help and guidance would be greatly appreciated.  Thanks
Logged
Pages: [1]   Go Up
  Print  
 
Jump to: