HowTo manage .htaccess
From Winhosting.com - web hosting and eCommerce solutions
Q. HowTo :: manage .htaccess file
- 1. What is .htaccess file?
.htaccess is an ASCII text document that can be placed in any directory on your Web site. The .htaccess file can be used to control access to files and directories, and customize server operation related to your Web site.
- 2. How to create a .htaccess file?
To create a .htaccess file, you need to use any word processor, but must be saved as text only.
You must upload the .htaccess file in ASCII mode.
- 2. Domain Redirection
You can add more Web sites to your main or primary Web site for FREE. Please, follow the following steps to add an additional Web site to your main or primary Web site:
- Your must park/point your additional domain name to our DNS servers. Please, contact your domain name registration company and ask them to update your domain name with our DNS.
- Both the primary and the additional domain names point to our DNS.
- You must create a directory with the name of the additional domain without the suffix dot extension such as: .org or .com or .net
- Domain name redirection only applies to the virtual server and has nothing to do with email.
- The following examples domain1 is assumed to be the primary Web site. Domain2 is the additional domain name.
Delete or comment out "" the ones that do not apply
This rule is for .com primary to .com additional
RewriteEngine on RewriteCond % !^[^.]+\.domain1\.com RewriteCond % ^www\.([^.]+)\.com RewriteCond % !domain2|domain3|domain4 RewriteRule ^(.*) %1/$1 [L] RewriteCond % !domain1\.com RewriteCond % ^([^.]+)\.com RewriteCond % !domain2|domain3|domain4 RewriteRule ^(.*) %1/$1 [L]
This rule is for .com primary to .net additional
RewriteEngine on RewriteCond % !^[^.]+\.domain1\.com RewriteCond % ^www\.([^.]+)\.net RewriteCond % !domain2|domain3|domain4 RewriteRule ^(.*) %1/$1 [L] RewriteCond % !domain1\.com RewriteCond % ^([^.]+)\.net RewriteCond % !domain2|domain3|domain4 RewriteRule ^(.*) %1/$1 [L]
This rule is for .net primary to .com additional
RewriteEngine on RewriteCond % !^[^.]+\.domain1\.net RewriteCond % ^www\.([^.]+)\.com RewriteCond % !domain2|domain3|domain4 RewriteRule ^(.*) %1/$1 [L] RewriteCond % !domain1\.net RewriteCond % ^([^.]+)\.com RewriteCond % !domain2|domain3|domain4 RewriteRule ^(.*) %1/$1 [L]
This rule is for .net primary to .net additional
RewriteEngine on RewriteCond % !^[^.]+\.domain1\.net RewriteCond % ^www\.([^.]+)\.net RewriteCond % !domain2|domain3|domain4 RewriteRule ^(.*) %1/$1 [L] RewriteCond % !domain1\.net RewriteCond % ^([^.]+)\.net RewriteCond % !domain2|domain3|domain4 RewriteRule ^(.*) %1/$1 [L]
Here is an example: We want to add ABC.com to XYZ.com. We don't want to pay monthly fees for ABC.com.
- Our main or primary Web site is: XYZ.com
- Our additional Web site is: ABC.com
Steps to add ABC.com to XYZ.com
- Update the DNS of ABC.com with the registration company.
- Send an email note to XYZ.com customer service requesting to park/point ABC.com to XYZ.com
- Create a directory by the name ABC in the public_html directory of XYZ.com
- Edit the .htaccess file found in the public_html directory of XYZ.com and inserted the following text:
RewriteEngine on RewriteCond % !^[^.]+\.XYZ\.com RewriteCond % ^www\.([^.]+)\.com RewriteCond % !ABC RewriteRule ^(.*) %1/$1 [L] RewriteCond % !XYZ\.com RewriteCond % ^([^.]+)\.com RewriteCond % !ABC RewriteRule ^(.*) %1/$1 [L]
Restricted Access
The following line is a catch-all. If the domain was not one of the above on the valid list then this statement will be executed. It assumes you have created an error document in the primary domain (domain1) root called deny.html Change .com to .net as required.
The second statement is used if you decided not to allow access by IP address, which means you deleted the condition for IP in the Host names section above. It also allows for a different message for the deny by IP. It could have been an "or" condition with the invalid subdomain if a standard deny message is used. Change .com to .net as required.
Invalid domain or Subdomain RewriteCond % ^(.+).domain1.com$ [NC] RewriteRule ^(.+) http://domain1.com/deny.html [R,L] Generate error on access by IP (replace with your IP) RwriteCond % ^xxx.xxx.xxx.xxx$ RwriteRule ^(.+) http://domain1.com/deny_ip.html [R,L]
URL Redirectshttp://otherdomain.com/*.* Redirect /directory1 http://otherdomain.com
The following is an example of simple redirects. A redirect can be to another directory or even a site on another host.
/directory1/*.* --> /derectory/sub/*.* /directory1/*.* --> /directory/sub/*.* Redirect /directory1 /directory2/sub /directory1/*.* -->
Change MIME-Types
Changing Mime-types is easy. Just create a .htaccess file that looks like this:
AddType new-mime-type xxx note the period is not used in the extension the above extension would be for .xxx files
The following is an example use of the .htaccess file. Let's assume then the path and filename of the .html file you would like to be that it resides at /home/username/public_html/.htaccess.
Prevent the browser from listing the content of a directory
The following will override default search order for an index file in a directory. Note the last entry is an error page. Also note the backslash /404.html. The error page must exist in your web root "/public_html". The reason that the index entries do not have a "/" is that we assume each subdirectory as a valid index file. This means you need one 404 file for the site. This 404 file will show the error page instead of a directory listing for directories without an index file. This is not the only method to prevent directory browsing. But we prefer it because it works by default instead of having to remember to put something in a directory.
- DirectoryIndex index.html index.cgi index.php /404.html
The following will override default search order for an index file in a directory. Note the last entry is an error page. Also note the backslash /404.html. The error page must exist in your web root "/public_html". The reason that the index entries do not have a "/" is that we assume each subdirectory as a valid index file. This means you need one 404 file for the site. This 404 file will show the error page instead of a directory listing for directories without an index file. This is not the only method to prevent directory browsing. But we prefer it because it works by default instead of having to remember to put something in a directory.
- DirectoryIndex index.html index.cgi index.php /404.html
Deny specific IP address(s) from accessing your Web site
The following line will deny an IP address from your site. They will receive a 403 forbidden error. This also can work with partial addresses. it will work with a domain only if reverse lookup is turned on. Notice that most dial up users get a different IP each time they logon. You need to replace the xxxxx with an actual IP address:
<limit GET POST PUT>
deny from xxx.xxx.xxx.xxx
</limit>
Create your own Error Web pages with any extension including *.php
If you wish to create and to use your own Error pages other than the *.shtml, use the following code in your .htaccess file
- ErrorDocument 404 http://your_domain/404.php
This can be true with any other suffix/extension including: *.jsp or *.phtml