This is not a manual, here you can find the most commonly used .htaccess settings, a kind of help collection.
Why do I need .htaccess
.htaccess allows you to create your own Apache server management configuration in directories or hosting settings.
The .htaccess rules apply to all directories where the file is located, except directories where your own .htaccess is located.
The .htaccess file is read by the Apache server every time it is accessed, so all changes take effect immediately, after the change.
The global settings of the Apache server may contain a ban on the execution of some commands, usually it causes a 500 error. Also, an incorrect syntax or error, for example, a space gap, can cause this error.
Deny access for specific IP addresses or ranges of IP addresses
The prohibition of access from the IP-address 123.123.123.123.
Order Deny,Allow Deny from 123.123.123.123
If you do not specify the last digits of the address, then the ban will extend to the entire range of 123.123.123.0 – 123.123.123.255.
Order Deny,Allow Deny from 123.123.123
We allow access only from certain IP addresses
Order Deny,Allow Deny from all Allow from 123.123.123.123
Force encoding instruction
AddDefaultCharset UTF-8
Sometimes you need to clear the browser cache.
Cancel server conversion
CharsetDisable On
Creating your own pages with error messages
When you go to a non-existent address, for example, the visitor sees a server error message, you can create your own error pages, or you can redirect the visitor to a different address, like the home page. But for the correct indexing it is desirable not to redirect, but to show the error page.
ErrorDocument 404 https://getranko.com/error/404.html ErrorDocument 403 https://getranko.com/error/403.html ErrorDocument 401 https://getranko.com/error/401.html ErrorDocument 500 https://getranko.com/error/500.html
The ErrorDocument string 404 http://site.ru/error/404.html indicates that 404 will show the 404.html file, which should be in the root of the site directory. If the file is located in a different directory, change the path to the file or link.
Redirects
Redirect to .html
Example, redirect with getranko.com/blog on getranko.com/blog.html.
RewriteCond %{REQUEST_URI} (.*/[^/.]+)($|\?) RewriteRule .* %1.html [R=301,L] RewriteRule ^(.*)/$ /$1.html [R=301,L]
Redirect to a page without a slash at the end of the address
Example, redirect with getranko.com/blog/ on getranko.com/blog.
RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.+)/$ /$1 [R=301,L]
Redirect to a page with a slash at the end of the address
RewriteCond %{REQUEST_URI} (.*/[^/.]+)($|\?) RewriteRule .* %1/ [R=301,L]
Redirect to a page without index.php in the address
RewriteRule ^index.php/(.*)$ http://getranko.com/$1 [R=permanent,L]
Redirect to a page without index.php at the end of the address
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/ RewriteRule ^index\.php$ http://getranko.com/ [R=301,L]
Redirect with www on without www (www.getranko.com to getranko.com)
RewriteCond %{HTTP_HOST} ^www\.getranko\.com$ [NC] RewriteRule ^(.*)$ http://getranko.com/$1 [R=301,L]
Redirect without www on www (getranko.com to www.getranko.com)
RewriteCond %{HTTP_HOST} ^getranko.com$ [NC] RewriteRule ^(.*)$ http://www.getranko.com/$1 [R=301,L]
Splicing domains
For example, you have several domains, but the visitor must be sent to one.
RewriteCond %{HTTP_HOST} !^getranko.com$ RewriteRule ^(.*) http://getranko.com/$1 [R=301,L]
Redirect from old static url to new ones
Example redirect from the page http://getranko.com/id=21.
RewriteCond %{QUERY_STRING} ^id=21$ RewriteRule ^/page.php$ http://getranko.com/new.html [L,R=301]
Protection from hotlinks
If you want to prevent the insertion of images from the site by a direct link.
Instead of site.ru, specify the site address, jpg | jpeg | png | gif – extension of forbidden images, images.jpg – image that will be displayed if the picture is not in the root of the site, specify the full path.
RewriteEngine on RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?getranko.com [NC] RewriteRule \.(jpg|jpeg|png|gif)$ images.jpg [NC,R,L]
Protection from bruteforce
We allow access to the administrator directory only via the HTTP protocol, which will filter out some bots. For each CMS you need to specify your address, for example wp-login, wp-admin and so on.
RewriteCond %{REQUEST_URI} ^/administrator\.php$ RewriteCond %{THE_REQUEST} HTTP/1\.0 RewriteRule ^(.*)$ - [F,L]
The material lists the most frequently used examples, and most of the features are not even mentioned, like the mod_rewrite module, which provides many features.