Knowledgebase : Tutorial

This tutorial will demonstrate how to use .htaccess files to perform several basic Apache and PHP configurations at the directory level. This tutorial makes the following assumptions:

  • You are using Apache as your webserver.
  • Your web host has enabled .htaccess override for your account.
  • For the PHP configuration, you must be running PHP as an Apache module.

If you're not sure about whether these statements are true, check with your web host's technical support department.

There several types of configuration that can be performed using a .htaccess file. These include authorization, redirects, changing the index file name, displaying custom error pages, and changing PHP configuration settings.

To perform such configuration, simply create a file named .htaccess (the dot in front is required) in the folder where you would like the settings to be applied. Any settings you configure for this directory will also be applied recursively to all of its sub directories. Note that some FTP clients may be configured to hide files that start with a dot. If this is the case, you may need to reconfigure your FTP client to display these files.

Changing the index file name

The index file is the file that gets displayed automatically when a user browses to a directory. Historically, the index file is called index.html or index.htm. On a PHP powered site, you may want your index file to be named index.php. This can be accomplished by putting the following line in your .htaccess file:

DirectoryIndex index.php index.html

The directive above instructs Apache to use index.php as the index file if it exists, otherwise it should look for a file named index.html. If neither file exists in the requested directory, the user will usually get a directory listing.

Using a custom error page

Everyone at one point or another encounters the dreaded "HTTP 404 - File Not Found" error message. Perhaps the user followed a broken link, or mistyped the URL of the page they were looking for. The default error page is usually quite ugly. However, it is possible to use a custom error page for this or any other HTTP error. This can be done by placing the line below in your .htaccess file:

ErrorDocument 404 http://www.mysite.com/404.html

Replace "http://www.mysite.com/404.html" with the URL for the page you want the user to see when they request a missing file.

It is important that your custom "file not found" page returns the HTTP 404 status code, otherwise your site could be penalized by the search engines. You can do this by adding the following PHP code at the top of your 404 page.

<?php
header("HTTP/1.0 404 Not Found");
?>

Denying access to a directory

Sometimes, there may be directories on your website that the user shouldn't be able to directly request files from. For example, you may have a directory that stores data files for your scripts, or a set of PHP includes. Placing the lines below in the .htaccess file for that directory will block direct requests for those files:

Order Deny,Allow
Deny from all

The first line ensures that the deny directive is evaluated before any allow directives that may have been defined elsewhere in the directory hierarchy.

Redirecting requests

Over time, files on a website may get moved or renamed. Rather than letting the user to see a 404 error message when requesting the old file, you can redirect them to the new file using the following directive:

Redirect permanent /old.php http://www.mysite.com/new.php

The permanent keyword indicates that an HTTP 301 ("resource has moved permanently") status code should be returned to the web browser. Replace "old.php" with the path to the old file, and "http://www.mysite.com/new.php" with the URL to the new file.

PHP configuration

Lastly, .htaccess files can be used to change PHP settings when running as an Apache module. Such configuration is performed using the php_value and php_flag directives. Use the later when the setting takes a boolean value. For example, to turn off register globals from .htaccess, use the following line:

php_flag register_globals off

The following directive can be used to turn off magic quotes for all PHP scripts in a directory:

php_flag magic_quotes_gpc off

To enable or disable error reporting, use the php_flag directive to set the display_errors flag. Enabling this flag will cause error messages to be displayed in the browser, e.g.:

php_flag display_errors on

To hide error messages from the end user, simply disable the display_errors flag:

php_flag display_errors off

Many web designers prefer to build their websites in test folders and when their development is over to move their Joomla applications to the root folder of their hosting accounts.

For the purpose of this article let us presume that we have a Joomla 1.5 installed in the public_html/test folder in our account and we want to move it to the public_html directory so that it will be directly accessible through www.yourdomain.com.

This change consists of the following steps:

1. Move all of the files and folders from your Joomla folder to the new directory. In our case from public_html/test to public_html


2. Reconfigure your application. You should edit your configuration.php file and make the following changes in it:

Change: var $log_path = '/home/user/public_html/test/logs';
To: var $log_path = '/home/user/public_html/logs';

Change: var $tmp_path = '/home/user/public_html/test/tmp';
To: var $tmp_path = '/home/user/public_html/tmp';

Change: var $ftp_root = '/public_html/test';
To: var $ftp_root = '/public_html';

Change: var $live_site = 'http://www.yourdomain.com/test';
To: var $live_site = 'http://www.yourdomain.com';

 

3. Remove the content of your cache folder (public_html/cache in our case)
By default most web servers across the internet are configured to treat as PHP files only files that end with .php. In case you need to have your HTML files parsed as PHP (e.g .html)

you can do the following:

AddHandler application/x-httpd-php5 .html .htm .php

There are many ways to connect and update your files on your hosting account (FTP, Frontpage, Cpanel File Manager).  One of the most popular is Dreamweaver.  We have assembled this easy to use guide to connecting Dreamweaver to your account.

Will Dreamweaver work with my hosting account?
    Yes.  Dreamweaver will connect directly to your hosting account and will sync with it.  DW should be able to upload all of your files and you may use it to update your site easily.  Please make sure to know where your local web files are stores on your PC before starting this tutorial.  Or if you are creating a new website, you can choose any location on your PC to start a site with.
What initial data do I need?
Your domain name.  Your ftp username and password.  You should have all of this information from your welcome email that we sent you.  If you do not have this connect us ASAP.
CONNECTION PROCEDURE:
To use Dreamweaver you must connect your PC to our server using the Dreamweaver program.  The following covers that procedure.

connecting Dreamweaver to Cpanel

Lanch Dreamweaver on your PC.
Use the menu at the top and select SITE, then select MANGE SITES.
Click NEW SITE
Name your site anything, like myWebSite
ok now click on the ADVANCED TAB



You should see Local info and Remote info now.  Local info is where your website files reside on you PC, that is why it is called local.  Remote info is the info on your server account here.

You should already see your site name filled in.  Press on the icon to the right of Local root folder and find where your website files are on your PC.  If it is an existing website, choose that existing folder.  If it is a new website, create a new folder.  Do the same with the default images folder.  (Note that only you will know where your local PC files are).

For http:// address enter your domain name you registered with us.  IE if your domain is rabbit.com, you would enter http://rabbit.com

Ok your local info is now defined.  Now click on the REMOTE INFO tab.




Select REMOTE INFO
Set Access to mode FTP
FTP HOST:  Will be your domain name minus any http://www.  Ie if your domain name is rabbit.com then that is all you enter here.  Do not enter www.
Host Directory: For all Cpanel enabled accounts enter public_html/
make sure to include the backslash /
Username: Enter your master FTP username as stated in your welcome email
Password: Enter your master FTP password as stated in your welecome email
Click on SAVE PASSWORD so you don't have to re-enter it
Passive transfers: click this box ON (if you have connection troubles you can try clicking it off).
Firewall: You usually leave this checked off
SFTP: You usually leave this checked off
Checkin and checkout:  Use this option if you only want altered files to transfer to the server.  Leave it off if you want the entire site to transfer each time.

Click on test to test your logon.  If it does not work, try tweaing your Passive and Firewall settings.
If it works of press OK at the bottom of the page to exit this page.

dreamweaver setup cpanel

Ok now click on DONE and your project should appear in the RIGHT SIDEBAR of DreamWeaver as shown below.



You have successfully setup Dreamweaver to connect with the server.  If you run into any problems please contact support and we will assist you.

Ok now how do I FTP my webfiles to the Server?

In the right sidebar, shown above.  Select your project, IE myWebSite. 
Now click on the first PLUG icon (it is the little blue PLUG icon just to the left of the circular arrow icon.
If you get a POP-UP screen at this point requesting local or remote info then you did not correctly configure your connection.  Repeat the steps at the start of this page.  Assuming you do not get that pop-up page, Dreamweaver should now connect.
If you get no connection errors continue.




You should not be connected to the server.
To view the files on the remote server connect on the icon shown above,
This should open a view of the local server (your PC on the right) and the server (on the left).



Assuming you have not transfered files yet, or you have added or edited files, the remote side will not look like the local side. You need to SYNC your files so the PC looks like the server.
Click on SITE -> SYNCHRONIZE
You can then choose to SYNC specific files you highlighted in the file menu above or all of the files.



If you want to send ALL files from your PC to the SERVER select Put newer files to remote.
If you want to send ALL files from the server to your PC select put newer files to local.
Be careful of the DELETE REMOTE FILES option.  If selected it usually will delete
files that do not appear on on side or the other.
Click Preview
Click OK
Your PC and server should be in SYNC

You can also move just one file by selecting it in the sidebar menu and pressing on the UP or DOWN
arrows.
I see a file list when I goto my domain but not my website?
Make sure to name your welcome page index.html then upload it.  This will fix this.



iWHOST Support System