.htaccess files are extremely useful tools for modifying websites. They allow the administrator to easily create rules to help manage the site. They are also useful for optimising it in browser search results. See how to use .htaccess files to create the perfect website.
What is an .htaccess file?
Hypertext Access, abbreviated as .htaccess, enables website settings to be modified by a person with access to the server. It can be used, for example, on an Apache or NGINX server. The most popular website creation software, WordPress, runs on these servers. The .htaccess command is also used there.
A single .htaccess file adjusts the functions of the main website by default. If the website contains sub-pages, .htaccess must be configured for each of them separately. The basic file is placed in the root directory, e.g. index.php or index.html.
When do we use the .htaccess directives? Examples of use
The .htaccess file allows you to define more than a dozen basic commands. Each of these can be further customised for a particular website. As a result, it is possible to use it for hundreds of different website operations. The following examples are only some of the possibilities of using .htaccess.
The use of .htaccess makes it possible, among other things, to control user access to individual site resources. This uses the FTP protocol for file transfer. This allows the site administrator to determine whether users will be able to download other files from the site. An example of the use of this command is the ability to grant access or block the downloading of image files from the site. If it is allowed, the administrator can also set the priority of their download.
Address rewriting is also useful, especially from an SEO point of view. This function is called mod_write. It allows the administrator to shorten a long and difficult-to-remember website link to a shorter form. A similar option is to create domains without the WWW abbreviation - it is not mandatory. You can also use both domains, with WWW and without at the same time.
For SEO, .htaccess affects the loading speed of a website. This is important because this speed is measured by search engine algorithms. Based on this, they decide which pages should be ranked higher in the organic search results.
The .htaccess file is also a way of restricting access to the site for unwanted users. Instead of blocking their accounts, the administrator can exclude an Internet user based on the IP address of the device. This method is more effective because the person breaking the site's rules can create a new profile. The .htaccess file also makes it possible to block the country from which the users in question originate. 
Redirects in the .htaccess file
Coding a page using the .htaccess file allows redirection to other sites. An example of its use is the 301 message. Its display means that the content of the page in question has been moved to another location on the internet. The user does not have to do anything. In such a situation, the server will automatically update the link and take the Internet user to the correct address.
When using an .htaccess file, redirecting to another domain also applies to the configuration of a page displaying a 404 error. This message is displayed when a site is not working due to a lack of communication with the server. This does not always apply to the entire site. Sometimes, for example, only one subpage has a connection problem. An internet user who understands the 404 message may thus try to navigate to other sections of the website.
Redirects using .htaccess files are also used by administrators to secure data. Banks, payment operators and online shops, among others, use this. A transaction is only possible after the user has been redirected to a properly secured page. This can be seen in the address bar. The appropriate .htaccess rule triggers the https forcing, instead of the unprotected http protocol. Of course, redirection alone does not ensure protection. It is up to cybersecurity specialists to properly secure the site.
Changes to the PHP version in the .htaccess file
The PHP programming language is extremely important for any website administrator. It allows for the automation of the content presented to internet users. An example of PHP's use is the display and updating of graphics appearing on a website. Without this programming language, administrators and webmasters would have to describe each change manually, modifying the HTML code of the page. With PHP, they can modify the content by entering only the necessary data.
Thanks to PHP, CSS sheets for formatting the website for the size of a given screen are also automated. The PHP language is responsible for running JavaScript. These are the scripts that determine the operation of, for example, the navigation elements of a page or the forms filled in when a user creates an account or makes a transaction by a shopper.
PHP code is also very useful because of its ease of use. Unfortunately, this can be exploited by cybercriminals. Once a site has been hacked, they can place a malicious file to steal customer information, delete some data, add their own content and so on. To prevent this, it is essential to change the PHP version regularly. This can be done using the command in .htaccess.
Upgrading PHP to a new variant also offers other benefits. It makes the website run faster, allows new features to be implemented and ensures that the website works correctly. The last point is particularly important. Changing PHP version e.g. from 7 to 8 will avoid errors on the website due to the backward compatibility of successive editions of this programming language. Changing the PHP version via .htaccess from e.g. 5 to 8 may cause technical problems that are difficult to resolve. 
Other .htaccess file options
Administrators of complex websites can benefit from the .htaccess option. From the SEO point of view, it is important to direct the Internet user on the basis of the phrase entered in the search engine. This makes it possible to define the start page entered in the .htaccess file.
The homepage does not necessarily mean the home page. They can exist simultaneously. With the right command, the administrator can redirect a user who wants to link to the homepage of the website to the start page. However, if his/her goal is, for example, to be on a subpage with the definition of a particular word, he/she can be helped to get to this information directly. The .htaccess file allows you to configure the start page.
With .htaccess, the site administrator can start listing the contents of the directory. This function is particularly useful on sites where content is searched for based on specific criteria. Online shops, for example, make use of this. Enabling the listing function gives the customer the chance to find a product based on its features.
What should a correct .htaccess file look like?
A simple text editor is sufficient to create the .htaccess file. Make sure that the text is encoded in UTF-8 format. It is necessary to grant the 644 permissions. These allow the file to be read and written. In this case, only the owner of the document has access to it.
When writing a command from within the system, it is worth being careful about the title given to the file. It should not contain a dot at the beginning. Windows will then recognise it as a system file and hide it. Without it, however, the command will not work when overwritten on the server. For this reason, the file name must be modified before saving to it. This rule does not apply to the creation of .htaccess commands in macOS.
Creating .htaccess commands can be difficult, especially for beginners. The solution is generators for such files. Many websites offer such a facility. After writing the commands in the system text editor or creating them in the generator, the file should be overwritten on the target site using one of the FTP programs. It is placed in the root directory, i.e. the aforementioned index.php or index.html.
.htaccess file - syntax
The correct construction of .htaccess commands requires consideration of the difference between uppercase and lowercase letters. This is because a command written using one or the other character can trigger other functions. Each rule should be written on a separate line.
The .htaccess commands can be disabled without deleting them. All you need to do is add the # sign at the beginning. This allows you to quickly restore the rule without having to write it from scratch. Examples of how to create an .htaccess file are available in many online tutorials.
Writing rules in .htaccess files is straightforward. Thanks to this uncomplicated tool, even an intermediate user can cope with administering the https force page. It is worth acquiring knowledge in this area in order to create websites that are functional for both the creator and the internet user.
Choose InPost Subscriptions for businesses and send parcels at a fixed price.
Take one minute to leave your contact details and start sending parcels from as low as 11.89 PLN*.
- One contract for courier and Paczkomat shipments
- Free courier pickups
- Express delivery
- Price guarantee for the duration of the contract
- Fuel surcharge included
- Dedicated post-sale support
* Net price per Paczkomat shipment in the Subscription 600 plan.
Czytaj również