http://Check-these.info/hosting/Generic_htaccessMethod.html


Generic .htaccess method for sub/pointed domains

Terminology used in this page

(by OPS -> package -> "add pointed domain" or "add serviced domain")


What is it?

Your package can have multiple domain names associated with it.
By default, all domain names and subdomains will act like alias of the main domain.
In other words, ALL traffic will go to the main /htdocs.

Example:
http://www.yoursite.com/index,html  ==> /www/U/USER/htdocs/index.html
http://sub1.yoursite.com/index,html ==> /www/U/USER/htdocs/index.html
http://www.pointed.com/index,html   ==> /www/U/USER/htdocs/index.html
http://sub1.pointed.com/index,html  ==> /www/U/USER/htdocs/index.html
http://sub2.pointed.com/index,html  ==> /www/U/USER/htdocs/index.html
http://www.serviced.com/index,html  ==> /www/U/USER/htdocs/index.html
 

Using RewriteRules (commonly known as mod_rewrite) in .htaccess file, you can specify different URL/directory/files to be served.

Example:
http://www.yoursite.com/index,html  ==> /www/U/USER/htdocs/index.html
http://sub1.yoursite.com/index,html ==> /www/U/USER/htdocs/sub1.yoursite.com/index.html
http://www.pointed.com/index,html   ==> /www/U/USER/htdocs/pointed.com/index.html
http://sub1.pointed.com/index,html  ==> /www/U/USER/htdocs/sub1.pointed.com/index.html
http://sub2.pointed.com/index,html  ==> /www/U/USER/htdocs/sub2.pointed.com/index.html
http://www.serviced.com/index,html  ==> /www/U/USER/htdocs/serviced.com/index.html
 

This way, you can use one package for serving multiple websites.


Warning

PowWeb DOES NOT give any assistance or support for using .htaccess.
Please do not file a support ticket about this matter.
Abusive use of support desk will slow it down and everybody may suffer.

Also, PowWeb will ask you to leave if you go over daily/hourly limits, often.
This method shouldn't be used by a site with lots of traffic.
Rather, you are encouraged to use multiple packages.

Using this method (or any other .htaccess based method) may complicate site maintenance.
Expect some undesired side effects if you use these.

Without "Virtual document root" feature, some script will act badly.
However, "Virtual document root" feature has it's own side effect and
it does add slight over head, too.

Please be aware that you are making a compromise.


Other methods

Currently, DomainManager does not support Generic .htaccess method explained in this page.
Rather, it offers "Traditional .htaccess method".
If you plan to have small number of sub/pointed domains, DomainManager can be a good choice.
It comes with diagnostic tools and it can handle PowWeb's new easy method subdomain, too.

You can setup everything manually.
There are many Forum posts and resources showing how to do this.
http://kb.powweb.com/ shows one way of doing it, but the code is not really good.
DomainManager is very useful for manual setup, too.
It has .htaccess editor, testing facility, latest log display link, and other features.

If you want to have subdomain, and you don't plan to use pointed domains,
PowWeb's Easy method subdomain can be a good choice.
However, you should know that it's a little difficult to share the files among this method of subdomain and main domain.
DomainManager fully support this.


How to?

Before to use ...

Make sure that pointed/serviced domains you intend to use are coming into main domain.

If not, follow these steps.
  1. Register the domain name (with http://registerfly.com/ , PowWeb , ...)
  2. Choose DNS (ns2.powweb.com & ns3.powweb.com if you don't know how to edit DNS record by yourself.)
  3. Goto OPS -> Packages and use "Add pointed domain" or "Add serviced domain" form to setup PowWeb's configuration. (If you make mistake in this step, you have to ask support to correct it, and it may take time.)
  4. Wait (up to 72 hours) for your DNS record to propagate. Check with "whois" (http://www.whois.sc/ http://www.dnsstuff.com/ ) "dig"
  5. Finally, http://newly-registered.com/ should show the same data as http://main-domain.com/

If you already have extratools.php,

  1. Click "Install/update genht.php"
  2. When automatic installation is done, click on the link to "run".
  3. Check the proposed .htaccess if you understand how it works.
  4. Click on one of "Setup ...." link.
  5. Create directories for sub/pointed domain you want
  6. Put some pages in the directories (such as index.html for testing)
  7. Test the new sub/pointed domains

If you have already a protected directory or you know how to protect,

  1. Upload or copy&paste this file as genht.php in the protected directory
  2. Access http://yoursite.com/protecteddir/genht.php
  3. Check the proposed .htaccess if you understand how it works.
  4. Click on one of "Setup ...." link.
  5. Create directories for sub/pointed domain you want
  6. Put some pages in the directories (such as index.html for testing)
  7. Test the new sub/pointed domains

If you are totally clueless,

  1. Login to WebFTP
    Access https://webftp.powweb.com/ using parameters you received in welcome mail. If you don't know which FTP server to use, goto OPS->Packages->FTP and you will see Server Hostname ftp0X.powweb.com
  2. Go to htdocs
    When the list of directories and files shows up, click on htdocs
    You should see You are here: /htdocs at the top of the list.
  3. Make new directory
    • Click Make Dir in the upper left side
    • Type in the name you can retain, all in lower case without space or other special characters ("zzz" for example).
    • Push Make Dir button.
  4. Go to newly created directory
    When the screen updates, click on the new directory you've just created.
    You should see You are here: /htdocs/newdir at the top of the list.
  5. Copy&Paste extratools.php
    • Click Create file in upper left side just under Make dir.
    • Open this URL: http://check-these.info/tools/extratools_php.txt in a new tab or window in your browser
    • Ctrl-A then Ctrl-C to copy everything. (or Edit menu -> select all, Edit->Copy)
    • Return to the Create file subwindow and paste with Ctrl-V
    • Type in extratools.php in the filename and push save
  6. Use extratools.php
  7. Install genht.php
    After logging in, click on install/update genht.php
  8. Use genht.php for setting up sub/pointed domains
    • When installation is done, click Access genht.php or run genht.php.
    • Click on "Setup Generic .htaccess method with virtual document root"
    • Read the out put, read the way you should create directories.
  9. Create directories for sub/pointed domains
    • With WebFTP, create directories for the sub/pointed domains by:
        1. Go into /htdocs, just like you did before.
          You should see "You are here: /htdocs".
        2. Click "Make dir" button
        3. Enter the domain name WITHOUT "www" as directory name.
          ex. www.abc,com ===> enter "abc.com"
          ex. sub1.xyz.com ===> enter "sub1.xyz.com"
        4. Hit the "Make dir" button in the form.
    • Put files in new directory (such as index.html), and test.
        1. Go into newly created directory.
          You should see "You are here: /htdocs/abc.com", for example.
        2. Use "Crate file" to make index.html for testing.
        3. Type in some text with the name of domain.
          ex. "Test index for abc.com"
        4. Type in the file name of "index.html" (all in lower case)
        5. Save the file and confirm.
  10. Test sub/pointed domains
    In the addres bar of your browser, type in "http://abc.com", for example.
    You should see the test index.html you've just created.
    If you don't see it, first, CLEAR the browser cache.
    And reload the page (By F5, CTrl-F5, or Reload button)

If it doesn't work go to the trouble shooting section, bellow.
  1. Populate your contents
    By using WebFTP or other software, put your contents in the directory you've created for sub/pointed domains.

You can always ask assistance in PowWeb forum if you have problem.


The source code of the setup script

/tools/genht_php.txt

<?
#
# genht.php
#
#  Note:
#    If you already have sub/pointed domain with .htaccess,
#    probably you will need to edit .htaccess manually,
#
#    If you are using DomainManager, I recommend just leep using it
#    unless you are willing to convert everything to this method
#    or to handle delicate arrangement of  RewriteRules.
#
#  1. Put this file in a protected subdirectory under main /htdocs,
#       /www/U/USER/htdocs/protected/genht.php
#     using WebFTP, sitemanager, or other utility.
#     Note. You can use sitemanager, or other tools to protect a directory.
#  2. Access http://yoursite.com/protected/genht.php
#  3. Examin "Proposed .htaccess code"
#  4. If it looks fine, click one of the link to setup.
#  5. Remove this script.
#  6. Creat directory that matches desired sub/pointed domain name.
#   /www/U/USER/htdocs/  <== main /htdocs
#   /www/U/USER/htdocs/sub.domain.com/  <== directory for sub.domain.com
#   /www/U/USER/htdocs/2nd-domain.com/  <== directory for 2nd-domain.com
#   /www/U/USER/htdocs/sub.2nd-domain.com/  <== directory for sub.2nd-domain.com
#
#  Please read http://check-these.info/hosting/Generic_htaccessMethod.html
#  for more details.
#

$ver = "0.4";
$title ="Generic .htaccess method setup";

echo <<<END_HEADER
<html><head>
<title>$title</title>
<body bgcolor=#eaeae7>
<h1>$title</h1>
<hr>
<blockquote>
END_HEADER;

$user = get_current_user();
$domain = $_SERVER['HTTP_HOST'];

preg_match('/^(www\.)?([^.]+)\.(.*)$/',  $domain, $m);
$maindomain = $m[2];
$tld = $m[3];


$htname = $_SERVER['DOCUMENT_ROOT'].'/.htaccess';

$vdrcode = <<<END_VDR
# Virtual document root processing for CGI/PHP
RewriteRule ^/*virtualdr.cgi - [L]
RewriteRule ^/*[^./]+\.[^/]+/.*\.(cgi|php5?)\$ virtualdr.cgi [L]
END_VDR;

if(isset($_GET['doit']) && $_GET['vdr'] != 'yes'  ){
  $vdrcode = '';
}

$hta = <<<END_HTA
RewriteEngine on
RewriteBase /

$vdrcode
# Loop stopping
RewriteRule ^/*[^./]+\.[^/]+/.*\$ - [L]

# Code for missing Trailing slashe problem 
RewriteCond s%{HTTPS} ^((s)on|s.*)$
RewriteRule ^/*(.+/)?([^.]*[^/])$ http%2://%{HTTP_HOST}/$1$2/ [L,R=301]

########
# Put most other RewriteRules that applies to all domains, here.
# (Force www, Remove www, Bad browser kick out, Anti-leech, ...)
########

# Sub/pointer domain rewrite code 
RewriteCond %{HTTP_HOST} !^(www\.)?($maindomain\.?$tld|$user) [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)\$ [NC]
RewriteRule ^(.*)$ %2/\$1 

########
# Put other RewriteRules that need prioror sub/pointed domain rewrite, here.
######

END_HTA;

$chta = file_get_contents($htname);
$bhta = preg_replace('/RewriteBase[^\n]*\n/mi', '', $chta);
$bhta = preg_replace('/RewriteEngine[^\n]*\n/mi', str_replace('$','\\$',$hta), $bhta);


if( ! isset($_GET['doit']) ){

  echo <<<END_MESS
<pre>
This script will modify .htaccess so that you can manage sub/pointed/serviced domain
by simply adding/deleting directory with corresponding name.

Please read <a href="http://check-these.info/hosting/Generic_htaccessMethod.html">http://check-these.info/hosting/Generic_htaccessMethod.html</a>
for more details.

  If you are currently using <a href="http://check-these.info/DomainManager.html">DomainManager</a> and "traditional .htaccess method",
  I'd recommend you to stay with it.

When you click one of the link for setting up,
it will make a backup of current .htaccess with the date and time suffixed.
If you experience problem, you can simply delete .htaccess 
and rename the back up as ".htaccess".

It'll copy virtualdr.cgi if you choose the link "with virtual document feature".

Before to click the link for setup, take a look at "proposed .htaccess", bellow.

<a href="?doit=yes&vdr=yes">Setup Generic .htaccess method with virtual document root</a>

<a href="?doit=yes">Setup Generic .htaccess method without virtual document root</a>

<hr size=8 color=blue><h1>Current .htaccess:</h1>

$chta
<hr size=8 color=blue><h1>Proposed .htaccess (with virtual document root):</h1>

$bhta
<hr size=8 color=blue>


END_MESS;


}else{

  echo "<pre>Backing up '$htname' as '".$htname.'.'.date("Y-m-d_H-i-s")."'\n"  ;
  copy($htname, $htname.'.'.date("Y-m-d_H-i-s"))  ;
  echo "Updating '$htname'\n" ;
  $fh = fopen($htname,'w');
  fwrite($fh, $bhta);
  fclose($fh);
  chmod($htname, 0640);
  $vdr = file_get_contents('http://check-these.info/hosting/genericx.txt');
  if($vdrcode != ''){
    echo "Setting up '$vname'\n" ;
    $vname = $_SERVER['DOCUMENT_ROOT'].'/virtualdr.cgi';
    $fv = fopen($vname,'w');
    fwrite($fv, $vdr);
    fclose($fv);
    chmod($vname, 0700);
  }
  echo <<<END_DONE

<b>If you don't see any warning or error message, everything is done</b>

Now, created directories that matche the sub/pointed domain name you like.

   /www/U/USER/htdocs/                     <== main /htdocs
   /www/U/USER/htdocs/sub.domain.com/      <== directory for sub.domain.com
   /www/U/USER/htdocs/2nd-domain.com/      <== directory for 2nd-domain.com
   /www/U/USER/htdocs/sub.2nd-domain.com/  <== directory for sub.2nd-domain.com

END_DONE;

}
?>


Actual code used in this method

Code, with comment and instructions

Make sure to read this if you want to set it up, manually

generic.txt

######
# 
# Note: You can use http://check-these.info/tools/genht_php.txt
#       to setup this code, automatically.
#
# 1. For sub.domain.com, create a directory sub.domain.com
#    under the main /htdocs, using WebFTP, sitemanager or other tools.
#
#  /www/U/USER/htdocs/  <== main /htdocs
#  /www/U/USER/htdocs/sub.domain.com/  <== directory for sub.domain.com
#
#  /www/U/USER/htdocs/2nd-domain.com/  <== directory for 2nd-domain.com
#  /www/U/USER/htdocs/sub.2nd-domain.com/  <== directory for sub.2nd-domain.com
#
# 2. To use virtual document root feature, put following script into
#    main /htdocs as "virtauldr.cgi" and set permission to 700.
#      http://check-these.info/tools/virtauldr_cgi.txt
#
# 3. Copy & Paste this code into the .htaccess of main /htdocs,
#    Using WebFTP or other tools.
#          /www/U/USER/htdocs/.htaccess
#
#    And edit the line for skipping main domain, bellow.
#    You need to know the name of main domain, and main username.
#    These can be found in OPS -> Packages -> HTTP
#
# IMPORTANT:  Please be aware that making mistake in the .htaccess 
#             may cause general 500 internal server error.
#             Use WebFTP to edit again.
#             
#             Uploading .htaccess in wrong FTP transfer mode will cause
#             General 500 error, too. Use WebFTP to edit, 
#             as It will save the file in correct line ending (LF).
#
# Please check following page for more detail/update:
#   http://check-these.info/hosting/Generic_htaccessMethod.html
# 

RewriteEngine on
RewriteBase /

# Virtual document root loop stopper and rewrite code for CGI/PHP
# Remove these 2 lines if you don't use this feature.
#   You must have /www/U/USER/htdocs/virtualdr.cgi with chomd 700
#   for this feature to work.
RewriteRule ^/*virtualdr.cgi - [L]
RewriteRule ^/*[^./]+\.[^/]+/.*\.(cgi|php5?)$ virtualdr.cgi [L]

# Loop stopper code
RewriteRule ^/*[^./]+\.[^/]+/.*$ - [L]

# Code for missing Trailing slashe problem 
# (to avoid double login or missing www)
#
RewriteCond s%{HTTPS} ^((s)on|s.*)$
RewriteRule ^/*(.+/)?([^.]*[^/])$ http%2://%{HTTP_HOST}/$1$2/ [L,R=301]

########
# Put most other RewriteRules that applies to all domains, here.
# (Force www, Remove www, Bad browser kick out, Anti-leech, ...)
########

# Skip processing sub/pointed domain rules for;
#    MAINDOMAIN.COM, MAINDOMAINCOM.secure.powweb,com 
#    USER.secure.powweb.com, or USER.temp.powweb.com.
#
# Edit 'MAINDOMAIN', 'COM', and 'USER' to fit your account.
# DO NOT modify anything else, unless you know what you are doing.
# You can find main domain name and USER name in OPS -> Packages -> HTTP
#
# example: RewriteCond %{HTTP_HOST} !^(www\.)?(yoursite\.?com|username) [NC]
#
# If you want your main domain to be redirected to
# /www/U/USER/htdocs/MAINDOMAIN.COM, like other sub/pointed domains.
# comment out by adding # or remove this line
#
RewriteCond %{HTTP_HOST} !^(www\.)?(MAINDOMAIN\.?COM|USER) [NC]

# Sub/pointer domain rewrite code, main body
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$ [NC]
RewriteRule ^(.*)$ %2/$1 

########
# Put other RewriteRules that need prioror sub/pointed domain rewrite, here.
########

Code (without instructions)

genericx.txt

RewriteEngine on
RewriteBase /

# Loop stopping and Virtual document root processing for CGI/PHP
RewriteRule ^/*virtualdr.cgi - [L]
RewriteRule ^/*[^./]+\.[^/]+/.*\.(cgi|php5?)$ virtualdr.cgi [L]
RewriteRule ^/*[^./]+\.[^/]+/.*$ - [L]

# Code for missing Trailing slashe problem 
RewriteCond s%{HTTPS} ^((s)on|s.*)$
RewriteRule ^/*(.+/)?([^.]*[^/])$ http%2://%{HTTP_HOST}/$1$2/ [L,R=301]

# Sub/pointer domain rewrite code 
RewriteCond %{HTTP_HOST} !^(www\.)?(MAINDOMAIN\.?COM|USER) [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$ [NC]
RewriteRule ^(.*)$ %2/$1 

Virtual doc root

If you want to use virtual doc root feature, you must place
virtualdr.cgi in the /htdocs of Main domain,
and set permission of 700. (chmod 700)
/www/U/USER/htdocs/virtualdr.cgi

As this script has a built-in safety system, you don't have to worry
about putting it in the publicly visible directory.

This script does roughly same thing as Action directive, but changes DOCUMENT_ROOT, SCRIPT_URL, and a few other env variables in addition.
Since virtualdr.cgi acts after all URL to path transformation and SuExec process, it can be used for setting ENV variables otherwise wiped out by SuExec.
(TMP. TEMP, LD_LIBRARY_PATH, and so on)


Trouble shooting

IMPORTANT

Clear the browser cache and reload the page when you test.
If you don't do that, you can waste your time, a lot

VERY IMPORTANT

Remember to clear the browser cache and reload the page when you test.
Forgetting this may cause frustration and embarrassment.

Domain registration

DNS setup

It should resolve to the IP of your cluster.
http://www.whois.sc/ and/or http://www.dnsstuff.com/

Testing method

Env variables such as REDIRECT_STATUS, REDIRECT_URL may show you what's going on.

pe.cgi (set permission to 700)
#!/bin/sh
echo
printenv |sort

pe.php
 <pre><? echo `printenv |sort`; ?>

So, try a bogus URL, and check the error_log to see if the URL to path transformation is working as desired.

In other words. most problem are caused by fundamental lack of the knowledge
and/or simple mistakes.

Follow these steps EXACTLY for creating/editing .htaccess file
or for ensuring the file format and line endings are correct.

By saving the file with WebFTP, you can be sure that it has
correct line ending and character code.

1. Login to WebFTP.
https://webftp.powweb.com/
Put host name that came with welcome e-mail. ex. ftp05.powweb.com
Use the correct username and password for FTP

2. Click on htdocs in the list to go into the directory.
You should see You are here: /htdocs at the top of the list.

3. See if there is a file .htaccess

4. If it's there, click on "Edit" link.
If it's not there push the "Create file" button found in upper left side.

5. Copy & paste the code. Edit it following the instruction.

6. If you used "Create file" button, type in .htaccess as the filename.

7. Push "Save" button.

8. Clear the browser cache.

9. Test the URL, push reload button (or F5 in some browsers), to be sure.


Asking questions

When you ask questions about rewriterule or redirect:

Also, people with these blaming attitudes are known to experience more problem and keep them longer because it prevents them from identifying own mistakes and correcting them in a prompt manner.
We see too many example of this attitude in our Forum ... but it's more or less normal because forum is mainly for people who can't solve problem by themselves, and it attracts higher concentration of blaming people.

If you think these are too much, you can always hire somebody and they'll be happy to do things for you.


Back ground

As PowWeb offers 3 Free pointed domains and 3 more serviced domain, more users would be interested in using sub/pointed domains.

However, we may encounter some problems of script because we can't specify the document root for the given sub/pointer domain.
Few scripts will not work at all, while others may generate ugly URL.
(PowWeb', new easy method subdomainis the exception.)

In other words, the current pointed/serviced domains of PowWeb are more like something called "parked domain" by some other hosts.

Ideally, it's better if PowWeb has (dynamic) virtual host setup mechanism or any other system that let us set document root for each domains.

My backup host has this kind of system and it's very easy to use. You don't have to use .htaccess, you don't have to modify script, you just have to tell which directory should be used as the document root of the domain. Very simple.

But we don't know if PowWeb has intention of implementing such system, and I hate to wait for someone else to eliminate problem.
So I wrote yet another set of RewriteRules and wrapper.cgi.

I think this version is as close as we can get to the real sub/pointed domain based on virtual host or some other types of set up.

Note:
Although I've done lots of testing, this set up has secondary effects.

For CGI/PHP in sub/pointed domains created by this system
when you use virtual document root feature;
  • No 404 error_log entry will be produced for these CGI/PHP
  • 500 error_log entry will appear as that of virtualdr.cgi
  • ErrorDocument directives areignored for these CGI/PHP


Features & comparison

It's lighter than .htaccess Methods traditionally suggested in Tutorials if you have more than a few sub/pointed domains.

Much easier than .htaccess Methods traditionally suggested in Tutorials, especially when you have multiple sub.pointed domains.

Once setup, only thing you have to do to manage domains are creating/deleting directories for most cases.

The rule for creating directory is very easy, too!

For sub.domain.com, create a directory sub.domain.com
under the main /htdocs.
  /www/U/USER/htdocs/  <== main /htdocs
  /www/U/USER/htdocs/sub.domain.com/  <== directory for sub.domain.com

  /www/U/USER/htdocs/2nd-domain.com/  <== directory for 2nd-domain.com
  /www/U/USER/htdocs/sub.2nd-domain.com/  <== directory for sub.2nd-domain.com
By default, main domain, main secure URLs, and temp URL will go to /htdocs.
You can remove RewriteCond lines for escaping MAINDOMAIN.COM
if you want it to go /www/U/USER/htdocs/MAINDOMAIN.COM/
instead of /www/U/USER/htdocs/.

Because of this loop stopping method, you can't have a directory name
that contains dot (period '.') directly under /htdocs.

By using tiny shellscript wrapper CGI, you can avoid most of
these problems without modifying each script.

This feature can be turned off by commenting out/removing the code
if you don't need it.


Questionable color of this page is dictated by blueberry cream cake, my favorite dessert.

This page is http://Check-these.info/hosting/Generic_htaccessMethod.html

My main site is hosted by PowWeb, one of the best low budget host !

5,000 MB + 10 Gigs/Day Transfer - $7.77/mo Web Hosting!
Last modified: 2005-10-26_23:54:44   Powered by Wikiciter CMS