If you have ever created a website or are dedicated to the world of SEO, you have surely heard of the .htaccess file, even if you don’t know very well what it is. Don’t worry! Today at we break down what .htaccess is, what it is for and we also give you some keys so you can edit it and improve your website.
What is the .htaccess
The .htaccess (hypertext Access) is a file that defines the behavior of the server to user requests. That is, the server, before responding to the user’s request to a certain website, checks the .htaccess file, adjusting the response to the guidelines found in the file.
Where is the .htaccess file
The .htaccess modifies the directory and subdirectories in which it is located. The file must therefore be in the root folder of the directory to which it belongs. To access it, you must enter the archives of the website.
You can do it through the hosting you have contracted or through managers like Filezilla. But if touching these files makes you sweat or you don’t have access to hosting or FTP, don’t panic! You can do it from WordPress itself. We explain how!
Access the .htaccess through the hosting CPanel:
Once in the CPanel of your hosting, you will only have to access “File Manager”. Once there, you will find all the files of your website and you will have to look for the .htaccess in the root folder of the directory. Once downloaded and edited, you must upload it again, replacing the previous one.
Access the .htaccess through Filezilla:
Filezilla is a free, downloadable application for both Windows and Mac that connects to the FTP server and allows you to manage and access files. In addition to , you must have access to the FTP, which is configured through the hosting. To connect to FTP from Filezilla, open the application and select File > Site Manager > New Site.
Once you have entered your data in Filezilla and it has connected with your FTP, you only have to access the root folder (usually inside www or public_html) and download your file, double clicking on the .htaccess. Once downloaded and edited, you must upload it again, replacing the previous one.
On some occasions, it may be that you do not see, a priori, the .htaccess. This is because some servers have it hidden by default. You will only have to tell your server to show the hidden files and it will appear!
Access the .htaccess from WordPress
Another option, perhaps more comfortable especially for those who are not familiar with web file managers, is to do it from WordPress itself. However, in this case, a copy of the .htaccess will not be saved by default, so you will need to copy the .htaccess directives into a new document before making changes in WordPress to ensure you don’t lose anything if an error occurs. unexpected. To edit the .htaccess from WordPress itself, you can use tools like , how? It is very simple:
From the WordPress admin menu, click on the plugin Yoast SEO > Tools > File Editor
How to edit the .htaccess
Before starting to edit the .htaccess, you must take into account that it is a sensitive file that, if it includes an incorrect guideline, can generate errors on the web. For this reason, it is always advisable to make a backup file before editing.
To edit the .htaccess it is not necessary to have a special program. You can do it from notepad or any other text editor.
The recommended thing when editing the .htaccess, like other files that contain several directives (such as the css files or the header.php), is that comment on the rules to have everything organized and know the function of the rules that we put. To do this, you just have to put the # symbol at the beginning of the line containing the comment. If the comment spans multiple lines, it should be included at the beginning of each line. Ex:
#this
#it is
#a
#comment
Once the .htaccess is edited, upload it back to your website and voila!
What is the .htaccess file for?
The .htaccess file has numerous functionalities, since, as we have said, it establishes guidelines for the execution of the server in response to a request. We are going to see some of the most common ones and how to implement the instructions correctly.
Redirects in the .htaccess
One of the most common functions is the creation of redirects. Let’s see the most important.
#http to https redirect
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}
Redirect domain with www to non-www
#domain redirection with www to non-www
RewriteEngine On
RewriteCond %{HTTP_HOST} !^yourdomain\.com$
RewriteRule ^(.*)$ http://yourdomain.com/$1
Redirect a domain without www to with www
#domain redirection without www to with www
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1
Redirection from an old domain to a new one
#Redirect to a new domain
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourolddomain.com
RewriteCond %{HTTP_HOST} ^www.yourolddomain.com
RewriteRule ^(.*)$ https://yournewdomain.com/$1
eye! This redirect maintains the structure of the URLs that were in the old domain. If we wanted to redirect all the traffic from an old domain to the home of a new one we should use:
#Redirect all traffic to the home of another domain
redirectMatch 301 ^(.*)$ https://yournewdomain.com
301 redirect from an old URL to a new one
There are two options:
#option 1
redirect 301 /old-relative-url/ https://yourdomain.com/newurl
#option 2
RewriteRule ^/old-relative-url/$ /new-relative-url?
block access
The .htaccess can be used to block traffic from certain bots or from certain IPs, for example.
block bots/websites
#bot/website blocking
RewriteEngine on
RewriteCond %{HTTP_REFERER} blockeddomain1\.com
RewriteCond %{HTTP_REFERER} blockeddomain2\.com
Block IP
#Block access from certain IPs
Order deny, allow
Allow from All
Deny from XXX.XXX.XXX.XXX
Deny from XXX.XXX.XXX.XXX
Activate the cache for the images of your web
#Image cache for a month
Header set Cache-Control “max-age=2592000, public”
Modify 404 page
One of the recommended practices to improve the user experience is the personalization of the pages with a 404 response; that is, those that appear when the page does not exist. To display this custom page, once designed, you must put the following rule in your .htaccess:
#Show custom 404 page
ErrorDocument 404 /errors/yourpage404.html
Now you know how to locate and edit the .htaccess. From we have shown you only some of the useful directives that you can insert in the .htaccess, but its potential is much greater. If you have any questions, leave us your comment!