B&T's Tips & Scripts
How did you find this site?
  Search engine25 % 
  My webhost27 % 
  Another website29 % 
  Referral13 % 
  Other6 % 
The last vote was cast today
B&T's Tool Box
Password Generation
Length of Password
Include Numbers
Include Uppercase Letters
Include Lowercase Letters
Include Special Characters
Password Encryption for .htpasswd
Username
Password
Hex RGB Color Conversion
Hex #
RGB
Check HTTP status code
enter full url to be checked
Domain Name / IP Resolution
enter Domain Name or IP Address

B&T's Recommendations

Site5.com

Got (or getting) a Mac?

AppleChronicles.com

B&T's Tips & Scripts is a collection of Tips & Scripts to help you build a better website.  The Tips & Scripts are targeted towards the native Apache PHP MySQL environment.  Check back frequently as there will be additions and modifications.  Please check our terms of use.
B&T's Tips & Scripts is a safe website.
No pop-up ads and no evil downloads.
Search Tips & Scripts
as current the latest index of this site
Tag Definitions Date - most recent update
- for advanced users
- topic rating
php5 - requires php5 or later
Have you had enough programming and website building for a while?
Need a break to clear your mind?
Go to PrettyWorthless.com, where you will find nothing of value.


   Using .htaccess  

  1. .htaccess Basics Mar 9, 2006
  2. Domain & subdomain pointing May 12, 2005
  3. Automatic subdomains Jun 17, 2007
  4. Undefined subdomains Jul 20, 2008
  5. Prevent direct access to a pointer or subdomain directory Aug 15, 2006
  6. Forcing or eliminating the WWW. Feb 9, 2005
  7. Fixing the trailing slash problem May 11, 2005
  8. Friendly URLs Mar 21, 2007
  9. Rewrites and https Dec 18, 2007
  10. Blocking traffic to your website Jan 2, 2005
  11. Anti-leech May 25, 2005
  12. Fancy default index page Jan 25, 2005


   Using .htaccess/.htpasswd Password Protection  

  1. Setting up password protection Feb 9, 2005
  2. Encrypt your password for .htpasswd Dec 21, 2007
  3. Auto Password Change and Email Notification Dec 21, 2007 php5
  4. Auto generate .htpasswd file Dec 18, 2007 php5
  5. Passing user authentication information to PHP Jun 13, 2004


   Coding Tips  

  1. HTML "One Liners" Nov 13, 2005
  2. PHP "One Liners" Apr 5, 2007
  3. JavaScript "One Liners" Aug 25, 2007
  4. CSS examples Sep 9, 2006
  5. SQL coding examples Aug 29, 2006
  6. AJAX example Sep 24, 2006


   Uploads and Downloads  

  1. File Upload Script Dec 20, 2007 php5
  2. Custom Download Page Dec 14, 2007 php5
  3. Tracking and Securing Downloads Dec 20, 2007 php5


   Mail  

  1. Form Mail Aug 19, 2007
  2. Obfuscate your email address Nov 12, 2006
  3. Read and auto-process mail Nov 12, 2004
  4. Auto-responder Dec 18, 2007 php5
  5. Display your email message count Jul 30, 2004
  6. Setting up SPF Records Jan 4, 2007
  7. Specify a return-path for generated mail Jul 18, 2006


   Working with Images  

  1. Watermark images Jul 14, 2007
  2. Resize images (create thumbnails) Jul 14, 2007


   Frequently Requested Website Functionality  

  1. Visitor Counter Dec 14, 2007 php5
  2. 404 Error Page (with reporting and logging) Dec 20, 2007 php5
  3. CAPTCHA Aug 19, 2006
  4. Poll (voting) Dec 17, 2007 php5   see it on this page
  5. Weather Jul 27, 2008 php5
  6. FAQ Page May 23, 2005
  7. Random/weighted banners, quotes & more Feb 9, 2005
  8. Load a random image (simple) Jan 20, 2004
  9. Image (banner) rotation Oct 15, 2007
  10. Simple "members only" pages May 13, 2005
  11. Replace banned words Apr 5, 2007
  12. Get the most recent file date Jan 30, 2006
  13. Moving (bouncing) Image Nov 10, 2005
  14. Sticky default values for an input Oct 22, 2006


   Using PHP  

  1. Creating a custom php.ini file Dec 18, 2007 php5
  2. Copying the php.ini file Dec 18, 2007 php5
  3. Deleteing php.ini files Dec 18, 2007 php5
  4. Clean up your PHP scripts Nov 11, 2005
  5. Regular Expression Rules Mar 22, 2006


   Website Managment  

  1. Tips for keeping your website secure Jan 24, 2006
  2. Using Cron Sep 13, 2005
  3. Website Monitoring Dec 19, 2007
  4. Automated Site Backups Dec 19, 2007
  5. Automated MySQL Backups Dec 19, 2007
  6. Server-to-server file transfer December 16, 2006
  7. MySQL Error Notification Dec 20, 2007
  8. Find File and Directory Sizes Dec 14, 2007 php5
  9. Deleting session files Aug 9, 2006
  10. FTP made easy Mar 1, 2006
  11. Search and Replace Dec 14, 2007 php5


   Other Tips  

  1. URL Validation Feb 8, 2008
  2. Stop direct links to web pages Jan 22, 2004
  3. Ensure your page is not opened in a frame May 5, 2005
  4. Masking your URL in the Address Bar Jul 20, 2004
  5. Ban or allow IP Addresses (without using .htaccess) Jan 19, 20008
  6. Using Cookies Sep 24, 2006
  7. View your website in different resolutions Jan 2, 2005
 

Back to Top     Print this Tip

Domain and subdomain pointing

Stop here!  If you want to do domain pointing, before you even start with htaccess code, make sure your pointed domain name resolves to your main domain name default page.  Many people think they have trouble with their htaccess code only to find the domain name is not properly pointed to their main domain in the first place.  So make sure your pointed domain name works, resolving to your default page, before going any further.  If you are doing subdomain pointing make sure that wildcard subdomains are enabled in your dns.  You can verify this by using any subdomain name and seeing if it resolves to your main domain default page.
The examples below assume that your root public directory (where you would put this .htaccess file) is htdocs, which is the default setup for Apache.   If your setup is different, then you may need to adjust the examples accordingly.

This approach has you code the first part of the domain or subdomain name and the associated subdirectory in your .htaccess file.   For example:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/
subdirectory/
RewriteCond %{HTTP_HOST} ^(www\.)?
name\.
RewriteRule ^(.*)$
subdirectory/$1 [L]

Note the second line has name, NOT name.com.   And note that there are some restrictions when using this method.
  • Domains and subdomains are treated exactly the same.  The pointing is based on the first argument of the url.  In this example name.com (as a pointed domain name) or name.domain.com (as a pointed subdomain) ends up at htdocs/subdirectory.   This is particularly useful in certain situations, for example if you want the subdomains of more than one domain to point to the same place, or if you have both a .com and a .net domain name you want pointed to the same place.
  • You must create a subdirectory under htdocs for every domain and subdomain you wish to be pointed.  The subdirectory name is then coded into the htaccess file.
  • The first argument in the url must be unique.  You cannot have two domains or subdomains with the same name.  sub.domain1.com and sub.domain2.com will both go to the same subdirectory.
  • It defaults to allow www. as a prefix on any domain or subdomain.
  • The root directory with this htaccess file is the "drop through" for any domain or subdomain not found.  You may want to put an error not found page as the default page.  Or, you could choose to have your main website be the "drop through" and just leave that one in the root.
There is also a simplified variation of this method that can be used in less complex situations where you are simply pointing a domain name and want ALL subdomains of that one domain to be pointed to a specific directory.   In this example name.com and any subdomain (including www) of that domain would be pointed to htdocs/subdirectory:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/
subdirectory/
RewriteCond %{HTTP_HOST}
name.com$
RewriteRule ^(.*)$
subdirectory/$1 [L]

In both examples, the htacess code works this way.   The first two lines set up the Apache server rewrite process.   Then you have two RewriteCond statements and a RewriteRule for EACH domain or subdomain you wish to point.
There is a second approach where pointed domains and subdomains become automatic, meaning no specific code is required for each pointer.   While this method is much easier to use and does not need to be modified when adding more domains or subdomains, note that there are some restrictions when using this method.
  • Domains and subdomains are treated exactly the same.  The pointing is based on the first argument of the url (except www if used).
  • You must create a subdirectory under htdocs with a - at the front for every domain and subdomain you wish to be pointed.  The directory name must match the first argument in the url (except for www if used).  For example, domain.com would have htdocs/-domain and sub.domain.com would have htdocs/-sub and domain2.com would have htdocs/-domain2  etc.
  • The first argument in the url, and therefore the directory name, must be unique.  You cannot have two subdomains with the same name.  sub.domain1.com and sub.domain2.com will both go to the directory htdocs/-sub
  • You cannot have any other directory names that start with a - character.
  • It defaults to allow www. as a prefix on any domain or subdomain.
  • The htdocs directory is the "drop through" for any domain or subdomain not found.  You may want to put an error not found page as the default page.  Or, you could choose to have your main website be the "drop through" and just leave that one in htdocs.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/-
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)
RewriteCond %{DOCUMENT_ROOT}/-%2 -d
RewriteRule ^(.*)$ -%2/$1 [L]

The htaccess code works this way.  The first two lines set up the Apache server rewrite process.  The first RewriteCond allows www. to be a prefix and captures the request name.  The second RewriteCond checks for the existence of a subdirectory with the request name.  The third RewriteCond ensures we are not caught in a looping operation.  The RewriteRule rewrites the request with the subdirectory name (which was already verified).
You can get more information on the Apache Module mod_rewrite here.

Rate This Topic



 
You are visitor 446,256
Last week there were 4,264 visitors
Copyright ©2004-2008