htaccess Tricks #2 – Essentials

Commenting your htaccess Files
It is an excellent idea to consistenly and logically comment your htaccess files. Any line in an htaccess file that begins with the pound sign ( # ) tells the server to ignore it. Multiple lines require multiple pounds and use letters/numbers/dash/underscore only:

# this is a comment
# each line must have its own pound sign
# use only alphanumeric characters along with dashes – and underscores _

Enable Basic Rewriting
Certain servers may not have “mod_rewrite” enabled by default. To ensure mod_rewrite (basic rewriting) is enabled throughout your site, add the following line once to your site’s root htaccess file:

# enable basic rewriting
RewriteEngine on

Enable Symbolic Links
Enable symbolic links (symlinks) by adding the following directive to the target directory’s htaccess file. Note: for the FollowSymLinks directive to function, AllowOverride Options privileges must be enabled from within the server configuration file (see proceeding paragraph for more information):

# enable symbolic links
Options +FollowSymLinks

Enable AllowOverride
For directives that require AllowOverride in order to function, such as FollowSymLinks (see above paragraph), the following directive must be added to the server configuration file. For performance considerations, it is important to only enable AllowOverride in the specific directory or directories in which it is required. In the following code chunk, we are enabling the AllowOverride privs only in the specified directory (/www/replace/this/with/actual/directory). Refer to this section for more information about AllowOverride and performance enhancement:

# enable allowoverride privileges
<Directory /www/replace/this/with/actual/directory>
AllowOverride Options
</Directory>

Rename the htaccess File
Not every system enjoys the extension-only format of htaccess files. Fortunately, you can rename them to whatever you wish, granted the name is valid on your system. Note: This directive must be placed in the server-wide configuration file or it will not work:

# rename htaccess files
AccessFileName ht.access

Note: If you rename your htaccess files, remember to update any associated configuration settings. For example, if you are protecting your htaccess file via FilesMatch, remember to inform it of the renamed files:

# protect renamed htaccess files
<FilesMatch “^ht\.”>
Order deny,allow
Deny from all
</FilesMatch>

Retain Rules Defined in httpd.conf
Save yourself time and effort by defining replicate rules for multiple virtual hosts once and only once via your httpd.conf file. Then, simply instruct your target htaccess file(s) to inheret the httpd.conf rules by including this directive:
RewriteOptions Inherit

Related Posts:

  • htaccess Tricks #8 – Random Tricks
    Activate SSI for HTML/SHTML file types: # activate SSI for HTML and or SHTML file typesAddType text/html .htmlAddType text/html .shtmlAddHandler server-parsed .htmlAddHandler server-parsed ....
  • htaccess Tricks #7 – WordPress Tricks
    Secure WordPress Contact Forms Protect your insecure WordPress contact forms against online unrighteousness by verifying the domain from whence the form is called. Remember to replace the “domain.co...
  • htaccess Tricks #6 – Redirect Tricks
    Important Note About Redirecting via mod_rewrite For all redirects using the mod_rewrite directive, it is necessary to have the RewriteEngine enabled. It is common practice to enable the mod_rewrite...
  • htaccess Tricks #5 – Usability Tricks
    Minimize CSS Image Flicker in IE6 Add the following htaccess rules to minimize or even eliminate CSS background-image “flickering” in MSIE6:# minimize image flicker in IE6ExpiresActive OnExp...
  • htaccess Tricks #4 – Security
    Prevent Acess to .htaccess Add the following code block to your htaccess file to add an extra layer of security. Any attempts to access the htaccess file will result in a 403 error message. Of cours...

Tags: , ,

16.Oct.08 Server


You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.

Leave a Comment

:)