Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I hope this is a simple YES or NO answer (please specify why)

Q1: Does is matter in what order the rules are placed in htaccess? Since they are completely separated items: for example

Q2: If yes, am I applying the right order? in order to speed up the htacces engine and not overload it with unneccessary rules?

Q3: any tips as to what to disable/add here are jucily welcome +1!


# DirectoryIndex index.php /index.php
AddDefaultCharset UTF-8
RewriteEngine on
# Options All
# Options +FollowSymLinks
# Options +FollowSymLinks -Indexes -ExecCGI
# RewriteBase /

#####################################################

<IfModule mod_headers.c>
    ExpiresActive On
    ExpiresDefault M172800
    Header unset ETag
    FileETag None
    Header unset Pragma

    ##### STATIC FILES
    <FilesMatch "\\.(ico|jpg|png|gif|svg|swf|css|js|fon|ttf|eot|xml|pdf|flv)$">
        ExpiresDefault M1209600
        Header set Cache-Control "public, max-age=1209600"
    </FilesMatch>

    ##### DYNAMIC PAGES
    <FilesMatch "\\.(php)$">
        ExpiresDefault M604800
        Header set Cache-Control "public, max-age=604800"
    </FilesMatch>
</IfModule>

#####################################################

#  /page123 and /page123/ will all go to /page123.php
RewriteRule ^(.+)/$  /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

####################################################

# NO WWW   http://www. becomes always http://
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

##############################################################
# add own extensions that will be interpreted as php
AddType application/x-httpd-php .php
AddType image/svg+xml svg svgz
AddType text/css css
AddType text/javascript js
AddEncoding gzip svgz

##############################################################

ErrorDocument 500 /
ErrorDocument 404 /
share|improve this question

3 Answers 3

up vote 5 down vote accepted

Well, .htaccess files use the same format as the regular Apache configuration file, so the same rules apply.

Most config settings do not depend on order, but some do - depends on the setting.

RewriteRule and RewriteCond e.g. are sensitive to order, so in that case the answer is YES.

See e.g.

http://wiki.apache.org/httpd/RewriteRule

for an explanation of the order that these are evaluated.

share|improve this answer

It does matter. Quoting from the documentation for RewriteRule:

The order in which these rules are defined is important - this is the order in which they will be applied at run-time.

share|improve this answer

I have the same concern posted but this is a server admin site perspective which allows them to restart apache after an apache server config change.

So far, the best response I've received is to list File related directives first.

This makes sense related to apache's need to manage directories and htaccess instructions in each directory.

So, list file related directives first, then obvious blocks to end the apache htaccess process in order of the obvious.

Possible solution to optimize the requests: - request url related corrections - Directory related restrictions - Index related restrictions - file related restrictions - proxy restrictions <-- kill all - empty user agent <-- kill all ... the list is endless fun

My concern related to sequence of directives. For instance, should I set Index, file, and Header directives before RewriteConds?

share|improve this answer
    
footnote: RewriteRule pattern substitution [flags] do Not answer this obvious application processing question! –  Testbench Feb 18 at 22:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.