How to optimize the .htaccess file Tutorials

How to optimize the .htaccess file

How to optimize the htaccess file with advanced parameters and achieve excellent web performance

 

Being able to optimize the htaccess file is very important when it comes to improving SEO positioning and to greatly increase the performance of your website. Below we show a number of codes that you can include in the htaccess file on your server.

 

Using GZIP (You must have the Gzip module installed on your Apache server)

This code is used to compress the information of your website to decrease the bandwidth consumption and facilitate the speed of loading the files of your website on the server, and therefore will decrease the loading time of the web.

<IfModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_item_include file .html$
mod_gzip_item_include file .php$
mod_gzip_item_include file .css$
mod_gzip_item_include file .js$
mod_gzip_item_include mime ^application/javascript$
mod_gzip_item_include mime ^application/x-javascript$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include handler ^application/x-httpd-php
mod_gzip_item_exclude mime ^image/.*
</IfModule>

 

Using DEFLATE (You must have the Deflate module enabled on your Apache server)

It is other code similar to the previous one that allows you to compress the information on the web using the Deflate module of the Apache server. We recommend this option before Gzip,because Deflate is much more compatible with older versions of browsers and is also a fairly fast compression.

AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml

 

Activate the Keep Alive resource

It allows you to keep the connections open between the client and the server, taking advantage of the server’s resources without having to waste them so that you can open a new connection for each item on the web page each time the client connects. This results in faster response speed when loading a web page.

Note: It is not recommended if you want to maximize the level of security of the server against cybercriminal attacks.

<ifModule mod_headers.c>
 Header set Connection keep-alive
</ifModule>

 

Specify a Cache duration value

The web cache allows us to save copies of the response data that a server sends to a client every time it connects to the web page for the first time, so the next time you connect, the data will be extracted from the saved cache and so you won’t have to request them to the server every time you connect to the web. This results in faster web loading speed the successive times the client connects.

<FilesMatch "\.(php|html|htm|txt|html5|jpg|png|js|css)$">
Header set Cache-Control "max-age=604800"
Header set last-modified "Tus May 15 13:00:00 EDT 2012"
</FilesMatch>
# Deshabilitar parámetros siguientes
<FilesMatch "\.(pl|cgi|spl|html)$">
 Header unset Cache-Control
 Header unset Expires
 Header unset Last-Modified
 FileETag None
 Header unset ETag
 Header unset Pragma
</FilesMatch>

The “max-age” data is expressed in seconds, so in this example the cache is set to 7 days, which are the days during which the web information will be saved to show it to the client each time it connects. The cache can be configured to last more or less days, depending on what each one wants.

 

Preventing Hotlinking

This code is ideal to prevent other websites from displaying images of your website from your own server. This happens when they use the same url of your image hosted on your server so that they can display it on another website without having it previously saved on their server. This causes your server’s bandwidth to be consumed in a totally inconsiderate and illegal way. Each time a user visits the web page where the images from your website appear, requests are being made to the server where the image is hosted, which in this case would be your server.

To prevent these cases, there is a solution that what it does is show an image in place of the “stolen” images from your server. So it is recommended that that image be a kind of warning that says for example “Image property of xxxxx” or some other warning that causes inconvenience to the person who is stealing bandwidth from your server, while at the same time notifying those who see that warning that from that time web page are stealing images that do not belong to that web server.

RewriteEngine On
#Change ?mysite\.com/ with our site url
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mysite\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
#Changes /images/nohotlink.jpg with an image that indicates that we do not tolerate hotlinks
RewriteRule .*\.(jpe?g|gif|bmp|png)$ /images/nohotlink.jpg [L]

It is recommended that the image you want to display is hosted on an external server, such as Imageshack or Tinypics as not to consume the bandwidth of our server.

 

Block IP addresses by creating a blacklist

In this way you can combat SPAM by blocking IP addresses that have junk content or that perform SPAM practices so that they cannot access your website.

<Limit GET POST PUT>
order allow, deny
allow from all
deny from 222.222.222.2
deny from 555.555.555.5
deny from 000.000.000.0
</Limit>

*Replace the example IP addresses with the IPs you want to block. You can add as many lines as you want with IPs to block.

 

Delete the “/” bar at the end of the URL

Many servers are configured to display a bar at the end of the URL (http://mysitio.com/blog/), so this can affect your Google site display if your Page “http://mysitio.com/blog/” does not carry the same content as “http://mysitio.com/blog” so this code causes all URLs on your website to be displayed without the bar at the end.

RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/$ /$1 [L,R=301]

 

Create a redirect to a new domain

This would be useful if you’ve moved all the content on your website to a new domain, and you want visitors who write the old domain to be automatically redirected to the new domain.

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.minuevodominio.com/$1 [R=301,L]

 

Create a custom 404 Error page

If you want the server to display your custom error page when a visitor accesses a site on your website that doesn’t exist.

ErrorDocument 404 /mipaginaerror404.html

 

Instructions for editing the htaccess file

In most cases, the htaccess file is created in the root directory of your website. Otherwise you would have to create a blank one to be able to insert the codes that we explained earlier.

  • Access your server files via FTP. You can use any FTP access software for this step, however we recommend the Filezilla for being one of the best we know. Find the htaccess file and hover over the file, right-click and then the “View/Edit” option.
  • The text editor opens with the contents of the htaccess file. Copy and paste the codes you want to use at the end of the file content. Remember to replace the code data with the data corresponding to your website, before pasting them into your htaccess file.

* In case you don’t have an htaccess file, you’ll need to create one and place it in the root directory of your website and then edit it simply with the codes you want to use and that we have shown you above. To create a new file, go to the root directory of your website through the Filezilla FTP software. Right-click inside the root directory and select “Create new file”. You name this file “htaccess” without the quotes, and you give it to accept. Once the file is created, you can edit it.

Artículos relacionados

This website uses both its own and third-party cookies to measure visits and sources of web traffic. The legal basis is the user's consent, except in the case of basic cookies, which are essential to navigate this website.
Need help? Chat with us
Please accept our Data Privacy and Polcily before starting the conversation.