CS · EN

Webhosting - .htaccess File

Do you need help with your website? Find your specialist at VEDOS Profi ⧉.

In this article, you will learn:


The .htaccess File

The .htaccess file (it must start with a dot) contains Apache server settings that control the operation of the web hosting service. The most common uses include routing in the web hosting directory structure, forcing HTTPS, redirecting web addresses, and disabling proxy cache.

Accessing the .htaccess File

To access the .htaccess file, proceed as follows:

  1. Log in to the hosting via FTP, for example using the WebFTP ⧉ client (Manual).
  2. Enter the www folder.
  3. Find the file named .htaccess among the files.
WebFTP - .htaccess file in the www folder
WebFTP – .htaccess file in the www folder

Rules in the default .htaccess file apply to all websites. Rules in .htaccess files in other directories (often, for example, www/domains/domena.tld) apply only to the contents of that directory and all of its subdirectories.

You can have more than one .htaccess file – it depends on the number of directories in which you want to set specific rules.


Restoring the Default .htaccess

If you cannot find the cause of an error caused by an edited default .htaccess file, or if you accidentally deleted this file, you can restore it directly in the customer administration.

  1. Log in to the customer administration ⧉.
  2. In the top menu, select Hosting Services Webhosting or WMS.
  3. Select the web hosting for which you want to restore the .htaccess file.
  4. In the left menu, click Tools.
  5. In the Restore default .htaccess table, click the restore .htaccess button.
Button for restoring the default .htaccess file
Button for restoring the default .htaccess file

At the same time as restoring the default .htaccess file, this button creates a backup of the current file, which the system copies into the same folder.

The button restores only the .htaccess file in the www folder. Other .htaccess files, such as those created by content management systems, must be restored in a different way.

The default .htaccess file does not contain rules for HTTPS. If you use HTTPS, add the rules to the restored file manually.

.htaccess Configuration

The conditions and commands in the .htaccess file use regular expressions. If you do not know how to use them, you can still edit the file, but pay close attention to the instructions. A non-functional .htaccess causes Error 500 – Internal Server Error.

Before you start editing the .htaccess file, create a backup of it.

In this chapter, you will find instructions for:

Routing in the Webhosting Directory Structure

The main purpose of our default .htaccess file is to route requests to the correct Webhosting folders, especially domains and subdom. If you notice errors in directory navigation, fix the .htaccess file so that it contains the following text, or restore it completely.

RewriteEngine On

# cele domeny (aliasy)
RewriteCond %{REQUEST_URI} !^domains/
RewriteCond %{REQUEST_URI} !^/domains/
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$
RewriteCond %{DOCUMENT_ROOT}/domains/%2 -d
RewriteRule (.*) domains/%2/$1 [DPI]

# subdomeny (s nebo bez www na zacatku)
RewriteCond %{REQUEST_URI} !^subdom/
RewriteCond %{REQUEST_URI} !^/subdom/
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)\.([^\.]*)\.([^\.]*)$
RewriteCond %{DOCUMENT_ROOT}/subdom/%2 -d
RewriteRule (.*) subdom/%2/$1 [DPI]

# aliasy - spravne presmerovani pri chybejicim /
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^domains/[^/]+/(.+[^/])$ /$1/ [R]

# subdomeny - spravne presmerovani pri chybejicim /
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^subdom/[^/]+/(.+[^/])$ /$1/ [R]

HTTPS Configuration

When activating HTTPS, you are also setting a .htaccess rule that selects the HTTPS variant as the default. Set it directly in the .htaccess file in the www folder, immediately below the RewriteEngine On line.

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R=301,L]
Header set Content-Security-Policy "upgrade-insecure-requests;"

Adding this code may block our application installer. If you encounter a problem with the .htaccess file during installation, temporarily restore it and after a successful installation overwrite it again with the automatically created backup.

The default .htaccess file does not contain rules for HTTPS. If you restored it and want to use HTTPS rules, add them again.

URL Redirects

With the following rule, known as a 301 Redirect, you are telling the browser and search engines that the page has permanently moved from the old address to a new one. Search engines in particular will then not treat the moved content as duplicate content.

Copy the text below and paste it into the .htaccess file below the RewriteEngine On line. Then adjust it according to these rules:

  1. In the text staraadresa\.tld, replace the text staraadresa with the address from which the redirect is performed, and replace tld with the correct extension. Make sure that the character before the . (dot) is the \ (backslash) character.
  2. In the text http://novaadresa.tld, replace novaadresa.tld with the address to which you are redirecting the website. If the new address uses https, also replace the text http.
  3. If you want to redirect to the default page of the new address, delete the $1 characters on the second line.
RewriteCond %{HTTP_HOST} ^(www\.)?staraadresa\.tld$
RewriteRule (.*) http://novaadresa.tld/$1 [R=301,L]

Restricting Access for Selected IP Addresses

In the .htaccess file, you can restrict access to certain (or all) files only from certain IP addresses. Here is an example of blocking access to the wp-login.php file (the login page of the WordPress administration):

<Files wp-login.php>
Order Deny,Allow
Deny from all
Allow from X.X.X.X
</Files>

You can adjust the code according to these rules:

  • In <Files ___>, you can specify either a specific file or the * character for all files in the given folder and subfolders.
  • Replace the address X.X.X.X with your IPv4 or IPv6 address.
  • You can specify multiple Allow from X.X.X.X directives, each on its own line.

Access attempts to the given file from other IP addresses will end with Error 403.

The only way to gain access to a file from a blocked address is to add that address to the allowed list, or to remove the code. Customer support does not perform either of these actions.

Disabling Proxy Cache

If you need to disable proxy cache for a website or part of it (directory, subdomain), insert this code into the .htaccess file:

# DISABLE CACHING
<IfModule mod_headers.c>
Header set Cache-Control "no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires 0
</IfModule>

.htaccess Restrictions on Webhosting

To prevent unwanted server behavior, we have set certain restrictions for the .htaccess file.

Using any prohibited command or parameter for the Options command will cause the entire website to stop working, which will manifest as Error 500 – Internal Server Error.

In the web server configuration, you may use the following command categories for htaccess:

  • AuthConfig
  • FileInfo
  • Limit
  • Indexes
  • Options*

Avoid using the following commands:

  • php_flag
  • php_value
  • ServerSignature
  • SetEnv
  • SetHandler
  • XBitHack
  • AddHandler
  • Options*

* For the Options command, you may use only the Indexes option; all others are prohibited.


Common Problems

Common problems with the .htaccess file include:

The New Settings Do Not Work

Problem: After changing the .htaccess settings, the new behavior does not appear, or the website stopped working completely. This usually includes Internal Server Error and Too Many Redirects.

Cause: It may be a naming or placement error of the .htaccess file, the placement of code in this file, or its correctness.

Solution: Perform a thorough check and fix the errors:

  1. Check that the file is in the correct folder (it does not have to be only www) and that it is named exactly .htaccess, including the dot at the beginning.
  2. Make sure the code is placed in the correct part of the file (for example, at the beginning after the RewriteEngine On command), and that all parts containing placeholder text (e.g. domena.tld) have been replaced with the correct text.
  3. If the file contains blocked commands, disable those lines by placing the # character at the beginning of the line.

Restoring the Default .htaccess Does Not Work

Problem: After restoring the default .htaccess, the website still behaves the same.

Cause: Either you are restoring the wrong .htaccess, or the website display is cached incorrectly.

Solution: Refresh the page without browser cache using the keyboard shortcut Ctrl+Shift+R.

If you need to restore the default state of the .htaccess file somewhere other than in the www folder (or the CMS .htaccess, for example WordPress), obtain it from the backup or the CMS website.

An Unknown Program Is Overwriting .htaccess

Problem: After changing the .htaccess settings, something soon overwrites it with unwanted code.

Cause: There is software on the web hosting service (usually a plugin, but it can also be malware) that generates its own code in .htaccess.

Solution: Find the source of the changes in the .htaccess file:

  1. Security plugins usually use .htaccess to block access to sensitive parts of the system, such as the CMS administration. When trying to access them, you often see Error 403: Forbidden. The solution is to reconfigure or disable the security plugin.
  2. CMS updates may introduce problematic code into their .htaccess, which most often causes Error 500 – Internal Server Error. The solution is to disable automatic updates and make a quick change to the .htaccess code immediately after a manual update.
  3. Malware commonly disrupts the display of the page or redirects it to an unwanted URL. The solution is a thorough malware cleanup of the website, which VEDOS unfortunately does not provide or support.

Frequently Asked Questions

How do I restore the default .htaccess of a CMS?

Before you start editing the .htaccess file, create a backup. If you do not have one, either reinstall the CMS or find the default .htaccess content for the same CMS version elsewhere. If everything else fails, contact customer support.

Where can I find advanced .htaccess commands?

We do not provide support for advanced .htaccess commands. Some can be found in community articles in the .htaccess category ⧉. When using instructions found online, keep in mind the .htaccess restrictions on Webhosting.