Forum Moderators: phranque

Message Too Old, No Replies

Advice for WP htaccess file

         

nootkan

1:36 am on Mar 13, 2015 (gmt 0)

10+ Year Member



Hello all, just wanted to ask the experts here about my htaccess file for wordpress below and whether it is efficient or redundant as I just copied and pasted from other posts to try and mitigate the google page speed warnings. Also could this post[webmasterworld.com ] be added to improve speed or is it too much? Thanks.

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^go/(.+) /index.php?red=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

### activate mod_expires
ExpiresActive On
### Expire everything else 1 day from when it's last modified
### (this uses the Alternative syntax)
ExpiresDefault "modification plus 1 day"
### Apply a Cache-Control header to index.html
<Files index.html>
Header append Cache-Control "public, must-revalidate"
</Files>
# compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

# Or, compress certain file types by extension:
<files *.html>
SetOutputFilter DEFLATE
</files>
<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

<IfModule mod_expires.c>
# Default directive
ExpiresDefault "access plus 1 month"
# My favicon
ExpiresByType image/x-icon "access plus 1 year"
# Images
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
# CSS
ExpiresByType text/css "access 1 month"
# Javascript
ExpiresByType application/javascript "access plus 1 year"
# php
ExpiresByType application/php "access plus 1 year"
</IfModule>

not2easy

2:35 am on Mar 13, 2015 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



The WP snippet up at the top is better at the end of everything else. A few questions to clear things up.

Is this the entire file? Is there a reason you are setting expires for .html files. I mean do you have html files on the same site outside of WP? Is there some place where you deal with www/non-www canonical URLs? Is this a static site being moved to WP? Is this all new, or carried over from another site?

nootkan

3:18 am on Mar 13, 2015 (gmt 0)

10+ Year Member



Yes this is the entire file. Set the expires for html files because the post in cpanel forums had it in it. Don't have html files outside of the wp site. Not sure what you mean about www/non www though. I do have both listed in my webmaster tools account as per googles instructions. www is the default. This is a new wp site on my own dedicated server with 20 pages of content. Thanks for taking time to help me out greatly appreciated.

not2easy

4:16 am on Mar 13, 2015 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



As a general practice, copying pieces of other people's htaccess files can hurt your site and your server efficiency. Think of it as a server's list of things to do before visitors can see your site - you don't want to have the server busy looking for html files if there aren't any. It is much better to take a little time to find out what those instructions mean so you can decide whether you want to use them or not. There are good and not so good places to get this kind of information, I recommend the Apache site to give me the best information on how to write instructions and there are links near the top here for the Library and Charter (You may need to look around (if it is not in the breadcrumb menu top left, it may be listed in a "Forum Options" menu). Reading those discussions can help a person understand what the "gibberish" instructions mean.

Congratulations on moving to a nice new dedicated server, this usually means you can bypass htaccess and use the more efficient httpd.conf for some of these settings. It should also let you skip some of those <IFModule tags because you can check whether the Module is available or not, and usually can turn on what you need instead of having the server check to see if it has a module to work with.

The WordPress snippet is an exception because while I know in theory that it "doesn't need" that envelope:
<IfModule mod_rewrite.c>

I know in practice that it does need that envelope because that is what WP uses to look for its instructions. Be careful not to edit that part of your htaccess, it can be overwritten in some core updates by WP - whatever WordPress gives you for your htaccess file should be off limits for changes.

The www/non-www should also be handled at your server, not just in GWT imho. If you can visit your site with and without the www, it can affect your success. There are examples of the code in the Library. There is some disagreement as to how important the www/non-www is. It is important to me because fixing it woke up "dead" sites that had no known penalties, but a few directories without their own "www". Coincidence? Maybe, but to me it is important. YMMV.

lucy24

7:35 am on Mar 13, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Set the expires for html files because the post in cpanel forums had it in it. Don't have html files outside of the wp site.

Do you have html files within the WP site? It's important to keep in mind that expiration headers are determined by the actual file, not its URL. Where do you have files whose real, physical filename is "index.html" on a WP site?

If the expiration for a particular filetype, such as html or images, is the same as the default, you don't need to say it twice. That's the whole point of having a default.

You can put multiple OutputFilter statements on a single line if you like:
AddOutputFilterByType DEFLATE type/blahblah thistype/otherblahblah thattype/thirdblahblah
but don't waste time listing filetypes that you don't actually use. And although it won't hurt to gzip html files, your host probably does that already. It's pretty standard.

Nothing in htaccess is obligatory. Well, except the part that your CMS requires, if you're using one. Everything else can be added bit by bit as you learn about it. For example
Options -Indexes

was probably a lot of people's very first htaccess directive.

nootkan

7:12 pm on Mar 15, 2015 (gmt 0)

10+ Year Member



Thanks for the replies however I am still not sure whether you are saying this htaccess code is alright to use or not. I've been to the Apache site and it is very confusing and hard to understand so I left and came here as well as other forums to see if someone could explain things in laymans terms.

I am still trying to learn how to manage my dedicated server also and it isn't easy either, but I am making progress in that regard through trial and error. I tried to make changes to the httpd.conf file but it cost me over 300 bucks to pay an IT guy to fix the problems it created so I stay away from that also until I can learn more about it.

As for the http:// and www. I can access my site from both as that is what Google states they want in WMT or so I interpret them to say. Are you saying that they are wrong?

After looking through my site I see no html files inside or outside of wordpress but I don't know whether I just remove the reference to html in the htaccess code or the whole line that each instance resides in.

I am also not sure what it meant as the "default" as I see no reference to the word default in the htaccess code.

Thanks again for your patience in helping with this as I believe the only way I am going learn is by getting advice from people who can better explain by using examples and providing tips that I can put to use to see how they work for me. Does that make sense?

not2easy

7:57 pm on Mar 15, 2015 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



The part referred to as "default" is the part that WP added when it was installed. The lines that begin with:
# BEGIN WordPress

and end with:
# END WordPress

Those lines in your htaccess file should not be altered because they reflect the setup, the settings that were entered during the install. It is not that they cannot ever be edited, it is that editing those lines without completely understanding what you are doing can alter the behavior of WP in unexpected ways. It can also be overwritten by WP itself during major updates. So it is best to leave it alone, but also best if it comes after your other lines in htaccess.

Google wants "both" versions in GWT because both versions are accessible. They are treated as two different URLs, two different sites that are very high in duplicate content. For that reason it is the common practice to decide which one you want and redirect all requests to that one version.

I understand you are trying to learn and it is easier to copy and paste things that other people post, but what works for one site may be problematic for another which is why it is best to understand what you want to do, then find the best way to do it. Then test to be sure it does what you had in mind. People are happy to help you but no one can know exactly what you need or want to do. My suggestion for your "Expires" is to search (maybe use "ExpiresByType" to search) and see the discussion for more details. Then ask if you are not sure. The code pasted in the first post is not right for your site as it sets parameters that were right for another site, but not yours.

lucy24

8:09 pm on Mar 15, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I can access my site from both as that is what Google states they want in WMT or so I interpret them to say. Are you saying that they are wrong?

You've probably misunderstood something. You do need to register both forms in WMT, because technically www.example.com and example.com are different host names, and you could have use of one but not the other. (I like to picture a weird inheritance case involving two brothers who don't speak to each other.) But you should then pick one form, with or without, and redirect the other one. Otherwise you've got Duplicate Content, literally, throughout the entire site.

I see no reference to the word default in the htaccess code.

Look again; you quoted it yourself.
ExpiresDefault etcetera

I am still trying to learn how to manage my dedicated server also and it isn't easy either, but I am making progress in that regard through trial and error. I tried to make changes to the httpd.conf file but it cost me over 300 bucks to pay an IT guy to fix the problems it created so I stay away from that also until I can learn more about it.

Idle query: Why do you even have a dedicated server at this point? There are basically two possible reasons: because your traffic warrants it, or because you like to keep a firm grip on the reins. The second one clearly doesn't apply. Do you have a very large site? That's "large" as in lots and lots of traffic, not in number of pages.

In the long run, you will want to shift most rules from htaccess to the server config file. The mere act of permitting htaccess (AllowOverride directive) creates more work for the server, because it now has to look for htaccess files all along the path on every single request-- several times in fact, at least once per override category. htaccess is useful when you're trying out new things, because you don't have to restart the server every time you change a syllable. But once you've got your own server, you should restrict htaccess to certain directories, for example a test site living in an obscure backwater of the server.

$300 sounds like a reasonable price for overhauling an entire config file. Couple of days ago a friend beseeched me to fix his new browser settings, which were going to an unintended home page. (Because, of course, he'd told it to.) On the way, it occurred to me that if I were not a friend, I would be charging him $80 just to walk in the door.

nootkan

1:33 am on Mar 21, 2015 (gmt 0)

10+ Year Member



Okay based on your replies I've made changes to the htacces file. Does this look better? Also I have checked my http:// and www. urls and the http:// redirects to the www.

I have a dedicated server because I design websites and a few of my clients were having problems with their hosts and asked me if I hosted sites and I had been thinking about it for a while as I own over 15 sites of my own so I took the plunge. Glad to hear that I didn't get burned by the IT guy as I really didn't know if that was a reasonable cost.

### activate mod_expires
ExpiresActive On
### Expire everything else 1 day from when it's last modified
### (this uses the Alternative syntax)
ExpiresDefault "modification plus 1 day"
Header append Cache-Control "public, must-revalidate"
</Files>
# compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

# Or, compress certain file types by extension:
<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file \.(txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

<IfModule mod_expires.c>
# My favicon
ExpiresByType image/x-icon "access plus 1 year"
# Images
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
# CSS
ExpiresByType text/css "access 1 month"
# Javascript
ExpiresByType application/javascript "access plus 1 year"
# php
ExpiresByType application/php "access plus 1 year"
</IfModule>

BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^go/(.+) /index.php?red=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

[edited by: phranque at 11:24 am (utc) on Mar 21, 2015]
[edit reason] unlinked url [/edit]