在 htaccess 中放置规则的顺序是否重要?

Sam*_*Sam 10 cache .htaccess apache-2.2

我希望这是一个简单的“是”或“否”答案(请说明原因)

Q1:规则在 htaccess 中的放置顺序有关系吗? 由于它们是完全分开的项目:例如

Q2:如果是,我是否应用了正确的顺序? 为了加速 htacces 引擎而不是用不必要的规则使其过载?

Q3:关于在此处禁用/添加什么的任何提示都非常欢迎+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 /
Run Code Online (Sandbox Code Playgroud)

sle*_*ske 10

嗯,.htaccess 文件使用与常规 Apache 配置文件相同的格式,因此适用相同的规则。

大多数配置设置不取决于顺序,但有些会 - 取决于设置。

RewriteRule并且RewriteCond例如对顺序敏感,因此在这种情况下答案是肯定的。

见例如

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

用于解释评估这些的顺序。


Fli*_*imm 6

这很重要。引用RewriteRule 的文档

定义这些规则的顺序很重要——这是它们在运行时应用的顺序。

  • 在 mod_rewrite 中,这很重要 - 是的。然而,OP 并没有专门针对 mod_rewrite,并且 OP 的“.htaccess”文件中还有来自其他模块的许多其他指令。简而言之,来自_不同_模块(以及在不同_容器_中)的指令独立并按照预定义的顺序执行,无论它们在配置文件中的明显顺序如何。 (2认同)