SEO AUDIT
Tips for dealing with the .htaccess file
Tips for dealing with the .htaccess file
The .htaccess file is a powerful and at the same time easy to use tool for controlling web servers. Great effects can be achieved with just a few lines of code. Some of the most useful settings are described here.
Most of the common web servers have one or more control files that are named .htaccess. These servers include all those that are NCSA-compatible, the abbreviation for “National Center for Supercomputing Applications”. The best-known representative of this server is certainly Apache, a successor to the NCSA HTTPS.
There can be one .htaccess file per directory on a web server. This file is usually located in the root directory of the respective domain. They are simple text files that can be created and edited with a simple editor. These files can be used to set up redirects, block access or create individual error pages. Some of the most interesting things that are possible via .htaccess are described in a current article in the new 1 & 1 digital guide .
Below are some of the highlights from this article.
Set up redirects
It happens again and again that a URL is no longer valid and therefore a redirect to a new URL has to be set up. This is the case, for example, when moving to a new domain or when renovating or relaunching a website.
Fortunately, redirects are very easy to define via .htaccess. For example, to set up a permanent 301 redirect from URL A to URL B, the following code is sufficient:
RewriteEngine On
Redirect 301 domain.tld/url-a.html domain.tld/url-b.html
Per RewriteEngine Onis initially responsible for forwarding Apache extension mod_rewrite enabled (which must be done only once and not for each redirect). The forwarding to be carried out is then defined – done.
Block IP addresses
Access to individual domains, IP addresses or even entire IP areas can be controlled via -htaccess. You can also combine do’s and don’ts to achieve exceptions:
Order deny,allow
Deny from .yahoo.com
Deny from 192.168
Allow from 192.168.101.102
Order The order of the interpretation is determined by the statement. In the current example, the part to be blocked is evaluated first, followed by the exceptions.
Forwarding from HTTP to HTTPS
A very topical issue is the conversion of websites to the secure TLS protocol (HTTPS) – not least for the reason that Google has promised ranking advantages for such websites . In order for the calls to the old, unencrypted URLs to be forwarded to the new HTTPS URLs after a switch to TLS, a forwarding can be defined via .htaccess:
RewriteEngine On
RewriteCond %{Server_Port} !=443
RewriteRule ^(.*)$ https://ihredomain.tld/$1 [R=301, L]
As already described above, the instruction RewriteEngine Ononly needs to be defined once in the .htaccess. The subsequent instructions in the example ensure that all requests to non-secure URLs are forwarded to the respective HTTPS variant (via 301 redirect, i.e. permanently).
Define your own error page
Even in the event of an error, one would like to offer users useful information. Unfortunately, many of the generic error pages that are served by web servers are not very attractive and are more of a deterrent than help. Instead, individual error pages should contain further links or a search function so that users can orientate themselves further.
Via .htaccess you can define what should happen in the event of an error – for example if a requested page is not available (404 error):
Error Document 404 www.domain.tld/verzeichnispfad/404.html
This instructs the web server to display the page under the specified URL in the appropriate case. The URL can be a separate page, but it can also be an external URL.