我昨天在WordPress网站上工作,主要是在Web性能优化能力(在共享主机上)。
有一次,我注意到我的.htaccess文件有冗余的,可能相互矛盾的指令:
# BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress # From the HTML5 Boilerplate # https://github.com/h5bp/html5-boilerplate/blob/master/.htaccess # Apache Server Configs v2.0.0 | MIT License # https://github.com/h5bp/server-configs-apache ... # BEGIN WordPress RewriteEngine on # # Unless you have set a different RewriteBase preceding this # point, you may delete or comment-out the following # RewriteBase directive: RewriteBase / # # if request is for image, css, or js file RewriteRule \.(gif|jpe?g|css|js|ico)$ - [L,NC] # or if URL resolves to existing file RewriteCond %{REQUEST_FILENAME} -f [OR] # or if URL resolves to existing directory RewriteCond %{REQUEST_FILENAME} -d # then skip the rewrite to WP RewriteRule ^ - [S=1] # else rewrite the request to WP RewriteRule . /index.php [L] # # END wordpress
我从HTML5 Boilerplate(省略号)省略了.htaccess的内容,因为我不认为代码是相关的(我几乎没有触及它,主要是使用它,它在我知道的足够的站点上工作得很好没有问题, 可以参考这里 。
由于.htaccess文件的顶部有默认的WordPress指令,底部有“改进”的指令,这些指令会适用于?
我search了Stackoverflow和这里,发现了一些关于这一点的问题,但没有真正回答指令的处理顺序。 文件顶部或底部的那个会被使用吗?
这是一个明确的具体例子,但是我最终得到的是带有重复代码和冗余的.htaccess文件,以至于比起只适用于这个实例的东西,更一般的“经验法则”的答案对我来说会更有用。例如,我经常用不同的语法结束gzip( compress / mod_deflate )指令,我不知道它们的顺序是否重要。
我认为这个网站是在Apache 2.2上,如果有帮助。
谢谢
自RewriteRule . /index.php [L] RewriteRule . /index.php [L]会捕获除/之外的每个请求,并且无论如何将会转到index.php ,第二组规则(文件的底部)将不会应用。
其实,这不是真的,但他们要做的就是跳过index.php重写静态文件(这不会发生)。
这可能是一个复制粘贴错误,或Wordpress的自动pipe理.htaccess文件(这样一个坏的function)做一些不应该的东西。 删除第二套规则,这是不准确的,不太清楚,只是混淆的事情。