Category Archives: Php

htaccess Tricks

htaccess is used for many custom configurations for a web site. This is a plain text file that should be placed in DocumentRoot of your web site to achieve variety of effects.

Various directives can be used in this file to different things. Generally htaccess is used for the password protection of the directories on a web site.

To use these features on a site, we need to create a file named .htaccess in plain test. It will take single directive per line. upload this file to your DocumentRoot and get the desired results according to the use of directives used.

This articles is about advance htaccess techniques such as redirection and php directives.

PHP & HTACCESS

In case of shared hosting, The site owner do not have root or administrator rights to change the php directives according to his site code. e.g Some developers like to use “safe_mode on” while others like “safe_mode off”.

Now what if in /etc/php.ini file has “safe_mode off” you need “safe_mode on” for your site. Since you don’t have root/administrator rights on shared server, you cannot change this in /etc/php.ini file on the server.

In this case .htaccess file is very important for you.

You have to use php_flag directive to set desired php flags for your sites DocumentRoot, no matter what is there in /etc/php.ini file.

If you want to use “safe_mode on” just use the following directive in .htaccess file

php_flag safe_mode on

similarly,

php_flag magic_quotes_gpc on

php_value register_globals on

Not only this you can also customize your DocumentRoot options other than the globle settings for your apache web server.

# AddEncoding x-gzip gz

i.e. your webserver is not using directive through its main configuration file, but you want to use this for your site, again .htaccess file will help you in this, just enter following to this file:

AddEncoding x-gzip gz

AddEncoding x-compress Z

Many such Directives can be used to enhanced the working features of your site.

Some important Directives, I am going to explain here.

Action

Syntax: Action mime-type cgi-script

This directive adds an action, which will activate cgi-script when a file of content type mime-type is requested. It sends the URL and file path of the requested document using the standard CGI PATH_INFO and PATH_TRANSLATED environment variables.

AddDescription

Syntax: AddDescription string file file …..

This sets the description to display for a file, for FancyIndexing. File is a file extension, partial filename, wild-card expression or full filename for files to describe. String is enclosed in double quotes (”).
Example:

AddDescription “The mother” /pics/earth.gif

AddEncoding

Syntax: AddEncoding mime-enc extension extension….

The AddEncoding directive adds to the list of filename extensions which filenames may end in for the specified encoding type. Mime-enc is the mime encoding to use for documents ending in extension.

Examples:

AddEncoding x-gzip gz
AddEncoding x-compress Z

This will cause files ending in .gz to be marked as encoded using the x-gzip encoding, and .Z files to be marked as encoded with x-compress.

AddIcon

Syntax: AddIcon icon name name ….

This sets the icon to display next to a file ending in name for FancyIndexing. Icon is either a (%-escaped) relative URL to the icon, or of the format (alttext,url) where alttext is the text tag given for an icon for non-graphical browsers.

Name is either ^^DIRECTORY^^ for directories, ^^BLANKICON^^ for blank lines (to format the list correctly), a file extension, a wildcard expression, a partial filename or a complete filename.
Examples:

AddIcon (IMG,/icons/image.xbm) .gif .xbm
AddIcon /icons/dir.xbm ^^DIRECTORY^^
AddIcon /icons/backup.xbm *~

AddIconByType should be used in preference to AddIcon, when possible.

AddIconByType

This sets the icon to display next to files of type mime-type for FancyIndexing. Icon is either a (%-escaped) relative URL to the icon, or of the format (alttext,url) where alttext is the text tag given for an icon for non-graphical browsers.

Mime-type is a wildcard expression matching required the mime types.
Example:

AddIconByType (IMG,/icons/sd.xbm) image/*

AddType

Syntax: AddType type ext

Context:.htaccess

The AddType directive allows you to add a mime type to your site.

Example:

AddType application/x-httpd-xx xx

ErrorDocument

Syntax: ErrorDocument error-code document

In the event of a problem or error, Apache can be configured to do one of four things,

behave like NCSA httpd 1.3
output a customized message
redirect to a local URL to handle the problem/error
redirect to an external URL to handle the problem/error
2-4 are configured using ErrorDocument, which is followed by the HTTP response code and a message or URL.

Messages in this context, begin with a single quote (”), which does not form part of the message itself. Apache will sometime offer additional information regarding the problem/error.

URLs will begin with a slash (/) for local URLs, or will be a full URL which the client can resolve.

Examples:

ErrorDocument 500 /cgi-bin/tester
ErrorDocument 404 /cgi-bin/bad_urls.pl
ErrorDocument 401 http://www2.foo.bar/subscription_info.html
ErrorDocument 403 “Sorry can’t allow you access today
The directive provides for access control by filename. It is comparable to the directive and directives. It should be matched with a directive. Directives that apply to the filename given should be listed within. sections are processed in the order they appear in the configuration file, after the sections and .htaccess files are read, but before sections.

unlike and sections, sections can be used inside .htaccess files. This allows users to control access to their own files, at a file-by-file level. When used in an .htaccess file, if the filename does not begin with a / character, the directory being applied will be prefixed automatically.

Redirect

Syntax: Redirect url-path url

The Redirect directive maps an old URL into a new one. The new URL is returned to the client which attempts to fetch it again with the new address.

Example:

Redirect /service http://foo2.bar.com/service

If the client requests http://myserver/service/foo.txt, it will be told to access http://foo2.bar.com/service/foo.txt instead.

RedirectTemp

Syntax: RedirectTemp url-path url

This directive makes the client know that the Redirect is only temporary. (Status 302). Exactly equivalent to Redirect temporary

RedirectPermanent

Syntax: RedirectPermanent url-path url

Context: directory, .htaccess

This directive makes the client know that the Redirect is permanent. (Status 301). Exactly equivalent to Redirect permanent

XBitHack

Syntax: XBitHack status

The XBitHack directives controls the parsing of ordinary html documents. Status can have the following values:

off

No special treatment of executable files.
on
Any file that has the user-execute bit set will be treated as a server-parsed html document.
full

As for on but also test the group-execute bit. If it is set, then set the Last-modified date of the returned file to be the last modified time of the file. If it is not set, then no last-modified date is sent. Setting this bit allows clients and proxies to cache the result of the request.htaccess is used for many custom configurations for a web site. This is a plain text file that should be placed in DocumentRoot of your web site to achieve variety of effects.
Various directives can be used in this file to different things. Generally htaccess is used for the password protection of the directories on a web site.

To use these features on a site, we need to create a file named .htaccess in plain test. It will take single directive per line. upload this file to your DocumentRoot and get the desired results according to the use of directives used.

This articles is about advance htaccess techniques such as redirection and php directives.

PHP & HTACCESS

In case of shared hosting, The site owner do not have root or administrator rights to change the php directives according to his site code. e.g Some developers like to use “safe_mode on” while others like “safe_mode off”.

Now what if in /etc/php.ini file has “safe_mode off” you need “safe_mode on” for your site. Since you don’t have root/administrator rights on shared server, you cannot change this in /etc/php.ini file on the server.

In this case .htaccess file is very important for you.

You have to use php_flag directive to set desired php flags for your sites DocumentRoot, no matter what is there in /etc/php.ini file.

If you want to use “safe_mode on” just use the following directive in .htaccess file

php_flag safe_mode on

similarly,

php_flag magic_quotes_gpc on

php_value register_globals on

Not only this you can also customize your DocumentRoot options other than the globle settings for your apache web server.

# AddEncoding x-gzip gz

i.e. your webserver is not using directive through its main configuration file, but you want to use this for your site, again .htaccess file will help you in this, just enter following to this file:

AddEncoding x-gzip gz

AddEncoding x-compress Z

Many such Directives can be used to enhanced the working features of your site.

Some important Directives, I am going to explain here.

Action

Syntax: Action mime-type cgi-script

This directive adds an action, which will activate cgi-script when a file of content type mime-type is requested. It sends the URL and file path of the requested document using the standard CGI PATH_INFO and PATH_TRANSLATED environment variables.

AddDescription

Syntax: AddDescription string file file …..

This sets the description to display for a file, for FancyIndexing. File is a file extension, partial filename, wild-card expression or full filename for files to describe. String is enclosed in double quotes (”).
Example:

AddDescription “The mother” /pics/earth.gif

AddEncoding

Syntax: AddEncoding mime-enc extension extension….

The AddEncoding directive adds to the list of filename extensions which filenames may end in for the specified encoding type. Mime-enc is the mime encoding to use for documents ending in extension.

Examples:

AddEncoding x-gzip gz
AddEncoding x-compress Z

This will cause files ending in .gz to be marked as encoded using the x-gzip encoding, and .Z files to be marked as encoded with x-compress.

AddIcon

Syntax: AddIcon icon name name ….

This sets the icon to display next to a file ending in name for FancyIndexing. Icon is either a (%-escaped) relative URL to the icon, or of the format (alttext,url) where alttext is the text tag given for an icon for non-graphical browsers.

Name is either ^^DIRECTORY^^ for directories, ^^BLANKICON^^ for blank lines (to format the list correctly), a file extension, a wildcard expression, a partial filename or a complete filename.
Examples:

AddIcon (IMG,/icons/image.xbm) .gif .xbm
AddIcon /icons/dir.xbm ^^DIRECTORY^^
AddIcon /icons/backup.xbm *~

AddIconByType should be used in preference to AddIcon, when possible.

AddIconByType

This sets the icon to display next to files of type mime-type for FancyIndexing. Icon is either a (%-escaped) relative URL to the icon, or of the format (alttext,url) where alttext is the text tag given for an icon for non-graphical browsers.

Mime-type is a wildcard expression matching required the mime types.
Example:

AddIconByType (IMG,/icons/sd.xbm) image/*

AddType

Syntax: AddType type ext

Context:.htaccess

The AddType directive allows you to add a mime type to your site.

Example:

AddType application/x-httpd-xx xx

ErrorDocument

Syntax: ErrorDocument error-code document

In the event of a problem or error, Apache can be configured to do one of four things,

behave like NCSA httpd 1.3
output a customized message
redirect to a local URL to handle the problem/error
redirect to an external URL to handle the problem/error
2-4 are configured using ErrorDocument, which is followed by the HTTP response code and a message or URL.

Messages in this context, begin with a single quote (”), which does not form part of the message itself. Apache will sometime offer additional information regarding the problem/error.

URLs will begin with a slash (/) for local URLs, or will be a full URL which the client can resolve.

Examples:

ErrorDocument 500 /cgi-bin/tester
ErrorDocument 404 /cgi-bin/bad_urls.pl
ErrorDocument 401 http://www2.foo.bar/subscription_info.html
ErrorDocument 403 “Sorry can’t allow you access today
The directive provides for access control by filename. It is comparable to the directive and directives. It should be matched with a directive. Directives that apply to the filename given should be listed within. sections are processed in the order they appear in the configuration file, after the sections and .htaccess files are read, but before sections.

unlike and sections, sections can be used inside .htaccess files. This allows users to control access to their own files, at a file-by-file level. When used in an .htaccess file, if the filename does not begin with a / character, the directory being applied will be prefixed automatically.

Redirect

Syntax: Redirect url-path url

The Redirect directive maps an old URL into a new one. The new URL is returned to the client which attempts to fetch it again with the new address.

Example:

Redirect /service http://foo2.bar.com/service

If the client requests http://myserver/service/foo.txt, it will be told to access http://foo2.bar.com/service/foo.txt instead.

RedirectTemp

Syntax: RedirectTemp url-path url

This directive makes the client know that the Redirect is only temporary. (Status 302). Exactly equivalent to Redirect temporary

RedirectPermanent

Syntax: RedirectPermanent url-path url

Context: directory, .htaccess

This directive makes the client know that the Redirect is permanent. (Status 301). Exactly equivalent to Redirect permanent

XBitHack

Syntax: XBitHack status

The XBitHack directives controls the parsing of ordinary html documents. Status can have the following values:

off

No special treatment of executable files.
on
Any file that has the user-execute bit set will be treated as a server-parsed html document.
full

As for on but also test the group-execute bit. If it is set, then set the Last-modified date of the returned file to be the last modified time of the file. If it is not set, then no last-modified date is sent. Setting this bit allows clients and proxies to cache the result of the request.