Mohammedz.com

For Linux and Shell scripting.

Introduction to .htaccess

2 Comments


.htaccess Definition.

Apache provides distributed, directory-level, configuration via Hypertext Access files. These .htaccess files enable localized fine-tuning of Apache’s universal system-configuration directives. Localized .htaccess directives must operate from within a file named .htaccess. The user must have appropriate file permissions to access and/or edit the .htaccess file. Further, .htaccess file permissions should never allow world write access — a secure permissions setting is “644”, which allows universal read access and user-only write access. Finally, .htaccess rules apply to the parent directory and all subdirectories. Thus to apply configuration rules to an entire website, place the .htaccess file in the root directory of the site.

Performance Issues
.htaccess directives provide directory-level configuration without requiring access to Apache’s main server configuration file (httpd.conf). However, due to performance and security concerns, the main configuration file should always be used for server directives whenever possible. For example, when a server is configured to process .htaccess directives, Apache must search every directory within the domain and load any and all .htaccess files upon every document request. This results in increased page processing time and thus decreases performance. Such a performance hit may be unnoticeable for sites with light traffic, but becomes a more serious issue for more popular websites. Therefore, .htaccess files should only be used when the main server configuration file is inaccessible.

Regex Character Definitions for htaccess

# instructs the server to ignore the line.
^ denotes beginning of a regular expression.
$ denotes end of a regular expression.
? declares the preceeding character as optional. i.e., it matches zero or one occurence of the

preceding character.
! denotes negation. i.e., “!hello” will match every string other than “hello”.
. a dot indicates any single arbitrary character.
+ matches one or more of the preceding character. e.g., x+ matches one or more x. “+” will

match one or more character of any kind.
* matches zero or more of the preceding character. e.g., you can use “.*” as a wildcard.
| logical “or” operator. for example, (x|y) matches x or y.
\ escapes special characters ( ^ $ ! . * | ).
^$ matches an empty string.
() used to group characters together, thereby considering them as a single unit.
[] specifies a character set, in which any character within the brackets will be a match. e.g., [xyz]

will match either x, y, or z.
[]+ character set in which any combination of items within the brackets will be a match. e.g.,

[xyz]+ will match any number of x’s, y’s, z’s, or any combination of these characters.
[^] specifies not within a character set. e.g., [^xyz] will match any character other than x, y, or z.
[a-z] a dash (-) between two characters within a character set ([]) denotes the range of characters

between them. e.g., [a-zA-Z] matches all lowercase and uppercase letters from a to z.

a{n} specifies how many times the preceeding character should match. e.g., x{3} matches x

exactly 3 times.
a{n,} specifies minimum number of matches for the preceeding character. e.g., x{3,} matches 3 or

more x’s
a{n,m} specifies minimum and maximum number of matches for the preceeding character.
-e tests if the file exists.
-f tests if string is an existing regular file.
-d tests if string is an existing directory.
-s tests if file exists and has a size more than zero.

Advertisements

2 thoughts on “Introduction to .htaccess

  1. its great really

    i have a question

    i sending a city name dynamically which contains spaces and “()” symbol.

    like

    http://www.flatshare.com/CAMPINAS%20(SP).flatSearchResult.html

    is there ant rewrite rule that i can set in my htaccess

    so that it will work fine

    i want to show like this

    http://sitename/city_name/searchresult.html

    i need help to write the following code

    RewriteRule ^([A-Z0-9._%+-]).flatSearchResult.html$ /output.php?city1=$1 [L]

    thanks in advance

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s