Notes and how-to’s for Apache

Some helpful config and other Apache notes:

  • redirect no hostname to “www”
  • RewriteEngine on
    RewriteCond %{HTTP_HOST}   !^(ww[w0-9]|m)\.DOMAIN\.com [NC]
    RewriteCond %{HTTP_HOST}   !^$
    RewriteRule ^/(.*)         http://www.DOMAIN.com/$1 [L,R]

    above will allow any host that starts with “www or ww[0-9] or m” – add or remove additional tests as needed.

  • Exclude real files and directories from an Apache rewrite to index.php can be done with .htaccess or config. Below is synax for Apache 2.2 config in httpd.conf (or an include file like httpd-vhosts.conf), which differs slightly from the .htaccess syntax. You may want to do this in config for performance and/or security reasons.
  • RewriteCond %{REQUEST_URI} !^/css/
    RewriteCond %{REQUEST_URI} !^/js/
    RewriteCond %{REQUEST_URI} !^/swf/
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-l
    RewriteRule ^/(.*)$	/index.php
  • remove trailing slash: (will remove trailing slash from URL except for root URL and redirect to page without slash)
  • RewriteRule ^(.+)/$ $1 [R=301,L]
  • When you want to show directory indexes, you often want to see the entire filename and better formatting:
    IndexOptions FancyIndexing NameWidth=* IgnoreCase FoldersFirst

Leave a Reply