0

I was working on a WordPress site yesterday, mostly in a Web Performance Optimization capacity (on shared hosting).

At one point I noticed that my .htaccess file had redundant, possibly contradictory, directives:

# 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 

I omitted the contents of the .htaccess from the HTML5 Boilerplate (the ellipses) as I don't think that code is relevant (I barely touch it, using it largely as is and it works just fine on enough sites that I know it isn't problematic. For reference, it can be found here.

Since the .htaccess file had the default WordPress directives at the top and the "improved" directives at the bottom, which would apply?

I searched Stackoverflow and here and found a couple questions on this point but nothing that ever really answered the order in which directives are processed. Would the one at the top of the file or at the bottom be used?

This is a clear, specific example but I end up with .htaccess files with duplicate code and redundancy often enough that a more general 'rule-of-thumb' answer would be more useful to me than something only applicable to this instance...for instance, I often end up with gzip (compress/mod_deflate) directives with different syntaxes and I have no idea if the order of them matters or not.

I think this site was on Apache 2.2, if helpful.

Thanks

1 Answer 1

1

Since RewriteRule . /index.php [L] will catch every request except /, and that'll go to index.php anyway, the second set of rules (bottom of the file) will never apply.

Actually, that's not quite true, but all they'll do is try to skip the index.php rewrite for static files (which wouldn't be happening anyway).

This was probably either a copy-paste error, or Wordpress' automatic management of .htaccess files (such a bad feature) doing something it shouldn't. Delete the second set of rules, it's less accurate, less clear, and just confusing matters.

2
  • As I understand it, on shared hosting there can be a decent performance improvement by telling the server not to query the database for static assets like CSS, JS, images...and that is what this is supposed to do. Is that incorrect (or negligible)? Commented Dec 2, 2013 at 7:14
  • @adam-asdf Pretty negligible, since it's just stating the file to determine if it exists. You'd get a much larger performance increase by disabling .htaccess files and moving this configuration into a <Directory> block. Commented Dec 2, 2013 at 17:55

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.