Knowledge Base›Webhosting›.htaccess File

Webhosting – .htaccess File

On this pageWhen to use this guideThe .htaccess fileRestoring the default .htaccessConfiguring .htaccessWebhosting .htaccess restrictionsCommon issuesFrequently asked questions

Need help with your website? Find your specialist on VEDOS Profi ⧉.

In this article, you will learn:


When to use this guide

Use this guide, for example, in the following situations:

  • A subdomain or another domain displays the content of the main website, or links lead to addresses with /domains/. See Subdomain displays a different website.
  • After editing .htaccess, you see Internal Server Error or Too Many Redirects. See New settings do not work.
  • A WordPress security plugin reports the error open_basedir restriction in effect for the .htaccess file. See Plugin reports open_basedir.
  • You need to restore the default .htaccess because you deleted or broke it, or you are setting up a new website in place of an old one. You will find the procedure in the chapter Restoring the default .htaccess.
  • You want to force HTTPS, redirect an old address or restrict access by IP address. See Configuring .htaccess.
  • A website, for example a page editor, loads scripts unreliably. You can try to turn off the proxy cache.

The .htaccess file

The file .htaccess (a leading dot is required) contains Apache server settings that control Webhosting operation. Its most common uses include routing in the Webhosting directory structure, enforcing HTTPS, redirecting web addresses and disabling the proxy cache.

Accessing the .htaccess file

To access the file .htaccess proceed as follows:

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

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

You can have multiple .htaccess files, depending on how many directories require specific rules.


Restoring the default .htaccess

If you cannot identify an error caused by changes to the default .htaccess, or accidentally deleted the file, restore it directly in Customer Administration.

  1. Log in to Customer Administration ⧉.
  2. In the top menu, select Hosting services Webhosting or WMS.
  3. Select the Webhosting whose .htaccess you want to restore.
  4. In the left menu, click Tools.
  5. In the table Restoring the default .htaccess click the button restore .htaccess.
Button for restoring the default .htaccess file
Button for restoring the default .htaccess file

Besides restoring the default .htaccess, this button backs up the existing file by copying it into the same folder.

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

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

Configuring .htaccess

Conditions and directives in .htaccess use regular expressions. You can edit the file even without knowing them, but follow the instructions carefully. A broken .htaccess causes Error 500 – Internal Server Error.

Before editing .htaccess, create a backup.

This section contains guides to:

Routing in the Webhosting directory structure

The main purpose of our default .htaccess is to route requests to the correct Webhosting folders, especially domains and subdom. If directory navigation fails, correct .htaccess to include the following text, or completely restore.

RewriteEngine On

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

# subdomains (with or without www at the beginning)
RewriteCond %{REQUEST_URI} !^subdom/
RewriteCond %{REQUEST_URI} !^/subdom/
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)\.([^\.]*)\.([^\.]*)$
RewriteCond %{DOCUMENT_ROOT}/subdom/%2 -d
RewriteRule (.*) subdom/%2/$1 [DPI]

# aliases - correct redirect when the trailing / is missing
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^domains/[^/]+/(.+[^/])$ /$1/ [R]

# subdomains - correct redirect when the trailing / is missing
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^subdom/[^/]+/(.+[^/])$ /$1/ [R]

HTTPS setup

Enabling HTTPS includes setting an .htaccess rule that makes HTTPS the default. Add it directly to .htaccess in the www folder, immediately below the line RewriteEngine On.

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 installation encounters an .htaccess problem, temporarily restore and, after successful installation, overwrite it again with the automatically created backup.

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

URL redirection

The following rule, known as a 301 redirect, tells browsers and search engines that a page has permanently moved from the old address to a new one. Search engines in particular will not treat the moved content as duplicate.

Copy the text below and paste it into .htaccess below the line RewriteEngine On. Then edit it using these rules:

  1. In the text oldaddress\.tld replace the text oldaddress with the source address, and tld with the correct extension. Make sure the character . (dot) is preceded by \ (backslash).
  2. In the text http://novaadresa.tld replace newaddress.tld with the destination address. If the new address uses https, also change http.
  3. To redirect to the new address’s default page, delete the following characters on the second line: $1.
RewriteCond %{HTTP_HOST} ^(www\.)?oldaddress\.tld$
RewriteRule (.*) http://newaddress.tld/$1 [R=301,L]

Restricting access to selected IP addresses

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

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

Modify the code using these rules:

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

Accessing the file from other IP addresses returns 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 changes.

Disabling the proxy cache

To disable the proxy cache for a website or part of it (directory or subdomain), insert this code in .htaccess:

# 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>

Webhosting .htaccess restrictions

To prevent unwanted server behavior, we impose certain .htaccess restrictions.

Using any disallowed directive or parameter of the Options directive will break the entire website, which will show up as Error 500 – Internal Server Error.

The web server configuration allows these directive categories for htaccess:

  • AuthConfig
  • FileInfo
  • Limit
  • Indexes
  • Options*

Avoid these directives:

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

* With the Options directive, only Indexesis allowed; all other options are prohibited.


Common issues

Common .htaccess problems include:

New settings do not work

Problem: After changing .htaccess, the new behavior does not appear or the website stops working entirely. Common errors include Internal Server Error and Too Many Redirects.

Cause: The problem may be the .htaccess file’s name or location, code placement within the file, or incorrect code.

Solution: Check thoroughly and correct errors:

  1. Check that the file is in the correct folder (not necessarily www) and named exactly .htaccess, including the leading dot.
  2. Make sure the code is placed correctly in the file (e.g. at the beginning after RewriteEngine On), and that all placeholder text (e.g. domain.tld) has been replaced with the correct text.
  3. If the file contains blocked directives, disable the relevant 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 behaves the same.

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

Solution: Reload the page without the browser cache using Ctrl+Shift+R.

To restore the default .htaccess outside www (or a CMS .htaccess, such as WordPress), obtain it from a backups or the CMS website.

An unknown program overwrites .htaccess

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

Cause: Software (usually a plugin, but it may also be malware) is installed on the web hosting and generates its own code in .htaccess.

Solution: Find what is changing .htaccess:

  1. Security plugins usually use .htaccess to block sensitive areas such as CMS administration. Access attempts then often produce Error 403: Forbidden. Reconfigure or disable the security plugin to resolve this.
  2. CMS updates may introduce problematic .htaccess code, most often causing Error 500 – Internal Server Error. Disable automatic updates and promptly adjust .htaccess immediately after manual updates.
  3. Malware commonly disrupts page display or redirects it to unwanted URLs. The solution is thorough website malware removal, which VEDOS unfortunately neither arranges nor supports.

Subdomain displays a different website

Problem: A subdomain or another domain displays the main website or main page, or links lead to addresses with /domains/, even though your website is uploaded in the correct folder.

Cause: The .htaccess file in the www folder does not route requests to the folders domains and subdom, or it is overridden by another .htaccess in the website directory. An incorrect website address in the content management system settings is also common (the database contains the address of the main domain instead of the subdomain).

Solution: In the www folder, restore the default .htaccess, or insert the code from routing in the directory structure. Check the directory structure according to the articles Webhosting – Subdomains and Webhosting – Restoring the Directory Structure (Reset) FTP. In the content management system, verify that the subdomain address is set. Finally, reload the page without the browser cache using the keyboard shortcut Ctrl+Shift+R.

Plugin reports open_basedir

Problem: A WordPress security plugin (e.g. Wordfence or Really Simple Security) reports open_basedir restriction in effect. File(/.htaccess) is not within the allowed path.

Cause: The open_basedir directive is blocked on the web hosting for security reasons, so the plugin cannot modify the .htaccess file.

Solution: Restore the WordPress .htaccess to its default form according to the WordPress documentation. If access via WebFTP does not work, connect with a desktop FTP client, for example FileZilla. Further steps are described in the chapter An unknown program overwrites .htaccess.


Frequently asked questions

How do I restore the default CMS .htaccess?

Before editing .htaccess, back it up. Without a backup, either reinstall the CMS or find the default .htaccess content for the same CMS version elsewhere. If all else fails, contact customer support.

Where can I find advanced .htaccess directives?

We do not support advanced .htaccess directives. You can find some in community articles in the .htaccess category ⧉. When using online guides, remember the Webhosting .htaccess restrictions.

I don’t have access via WebFTP. Will Support fix .htaccess?

First try connecting with a desktop FTP client, for example FileZilla. If you insist on having us do the fix, give Support temporary FTP access and send a request via the contact form ⧉.

My website loads scripts unreliably. Will turning off the proxy cache help?

It might, but it is not guaranteed. Insert the code from the chapter into .htaccess: Disabling the proxy cache. If it does not help, restore the settings to their original state.

Was this article helpful?

Your feedback helps us improve our articles.

Your message goes to the documentation team, who do not reply to it. If you need help, contact us using the contact form. Do not include passwords or personal data.

Didn't find what you were looking for?

Ask other customers on the Help community website.