With the help of the .htaccess file, various functions of an Apache web server can be used. Among other things, it can be used to set up redirects or control access to directories and/or files.
General
- The .htaccess file is a text file that contains instructions for a server.
- With the help of the .htaccess file, various functions of an Apache web server can be used.
- It is mostly used to set up redirects and is therefore also relevant for search engine optimization.
- It can also be used to block or restrict access from specific IP addresses.
- There are quite a few other use cases.
Where is the file stored?
The file does not exist from the beginning. The file is often created automatically by a CMS . If present, the file is usually found in the root directory of a web page.
Technical
Here we present a few example codes. In general, the Apache web server offers many more possibilities, but we will not go into them here, as this would be too complex and demanding and is already sufficiently documented elsewhere. If you need more complex code, you usually already know enough about .htaccess that you don’t need this article.
Minimal configuration with one redirection
RewriteEngine On
RewriteBase /
RewriteRule meineseite.html deineseite.html [R=301]
RewriteEngine On activates Apache’s mod_rewrite module and ultimately allows URLs to be rewritten.
RewriteBase defines the base directory to which the rewrite/redirect refers.
RewriteRule is the introduction of a redirection rule. First comes the origin and this is followed by the indication of where to forward to. The part in the square brackets describes the HTTP code that will be included in the server’s response. 301 stands for a permanent redirection, alternatively there is the code 302, which stands for a temporary redirection. These codes help e.g. search engines to optimize their results.
Example of a redirect rule
Redirect 301 /alte-seite.html https://www.domain.de/verzeichnis/neue-seite.html
Important here is the difference between RewriteRule and Redirect.
RewriteRule can be used not only for individual redirections, but also to cover an entire range by means of so-called regular expressions (regex).
Here is an example:
RewriteRule (.*) /somewhere/ [R=301]
With Redirect allows you to redirect one specific URL to another. In the second parameter, both paths and whole URLs can be specified, e.g. to redirect to an external page.
Example of a rule to block an IP address
order allow,deny
deny from 192.168.2.17
deny from 10.10.20.63
allow from all
This rule must be specified before theRewriteEngine Oncommand. Just replace the IP here with the IP address you want.
WordPress
For all of you who use WordPress, here is another example of the basic (default) .htaccess file for WordPress.
Standard code for a WordPress page
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress