In this article, you will learn:
- What is the .htaccess file for
- How to restore the default .htaccess
- How to set up the .htaccess file to:
- Which .htaccess commands are blocked
- Common issues
- Frequently asked questions
The .htaccess File
The .htaccess file (must include the dot) contains Apache server settings that control some parts of the Webhosting. The most common uses include routing in the web hosting directory structure, forcing HTTPS, redirecting URLs, and disabling the proxy cache.
.htaccess Access
To access the default .htaccess file, follow these steps:
- Log into your Webhosting FTP (such as via WebFTP).
- Navigate to the www folder.
- Find the .htaccess file.
The rules in the default .htaccess apply to every website on the service. Rules in .htaccess files in other directories (often for example www/domains/domena.tld) apply only to the contents of that directory and all 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.
Restore Default .htaccess
If you are unable to find the cause of the error caused by changing the default .htaccess file, or if you accidentally deleted this file, you can restore it directly in the customer administration panel.
- Log into the customer administration panel.
- In the navigation bar, select Web & Email Hosting Webhosting or WMS..
- Choose the webhosting to restore the .htaccess file for.
- In the left menu, click Tools.
- In the Restore Default .htaccess section, click Restore .htaccess.
At the same time as restoring the default .htaccess file, this button creates a backup of the existing file, which the system stores in 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 by other means.
The default .htaccess file does not contain rules for HTTPS. If you want to force HTTPS, add the rules to the restored file manually.
.htaccess Setup
The conditions and commands in the .htaccess file use regular expressions. If you are not familiar with them, you can still edit the file, but pay close attention to the instructions. Invalid .htaccess code causes Error 500 – Internal Server Error.
Before you start editing the .htaccess file, back it up.
In this article you will find instructions on how to:
Webhosting Directory Structure
The main purpose of our default .htaccess file is to direct requests to the correct Webhosting folders, especially domains and subdom. If you see directory navigation errors, fix the .htaccess file to include the following code, or restore it.
RewriteEngine On
# 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)
RewriteCond %{REQUEST_URI} !^subdom/
RewriteCond %{REQUEST_URI} !^/subdom/
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)\.([^\.]*)\.([^\.]*)$
RewriteCond %{DOCUMENT_ROOT}/subdom/%2 -d
RewriteRule (.*) subdom/%2/$1 [DPI]
# aliasses - missing /
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^domains/[^/]+/(.+[^/])$ /$1/ [R]
# subdomains - missing /
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^subdom/[^/]+/(.+[^/])$ /$1/ [R]
Force HTTPS
When you activate HTTPS, among other things, you may want to set a .htaccess rule that forces HTTPS. 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 cause our app installer to malfunction. 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 have restored it and want to use HTTPS rules, add them again.
Redirect URL
With the following rule, known as a 301 Redirect, you tell the browser and search engines that the page has definitely moved from the old address to the new one. In particular, search engines will not perceive the moved content as duplicate.
Copy the text below and paste it into your .htaccess file under the RewriteEngine On
line. Next, edit it according to these rules:
- In the text
oldaddress\.tld
, replace the text oldaddress with the address from which the redirect is taking place, and replace tld with the correct TLD. Make sure that there is a \ (backslash) character before the . (dot). - In the text
http://newaddress.tld
, replace newaddress.tld with the address the web redirects to. If the new address uses https, also overwrite the http text. - To redirect to the default page of the new address, delete the
$1
characters on the second line.
RewriteCond %{HTTP_HOST} ^(www\.)?oldaddress\.tld$
RewriteRule (.*) http://newaddress.tld/$1 [R=301,L]
Disable Proxy Cache
If you need to disable the proxy cache for the website or its part (directory, subdomain), insert the following 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
To prevent unwanted server behavior, we have set certain restrictions on the .htaccess file.
You may use the following .htaccess commands in your web server configuration:
- AuthConfig
- FileInfo
- Limit
- Indexes
- Options*
Avoid using these commands:
- php_flag
- php_value
- ServerSignature
- SetEnv
- SetHandler
- XBitHack
- AddHandler
- Options*
* You may only use the Indexes option for the Options command, the others are blocked.
Using any blocked command or parameter in the Options command will cause the entire site to throw the Error 500 – Internal Server Error.
Common Issues
Common problems with the .htaccess file include:
New Settings Malfunction
Issue: After changing the .htaccess settings, the website works as before, or stopped working completely. The most common errors include Internal Server Error and Too Many Redirects.
Cause: It can be a mistake in the naming or location of the .htaccess file, the location of the code in this file, or errors in the code itself.
Solution: Perform a thorough check and correct all the errors:
- Make sure the file is in the right folder (it doesn’t have to be just www) and is named exactly .htaccess, including the dot at the beginning.
- Make sure that the code is in the correct place in the file (for example, at the beginning after the
RewriteEngine On
command), and that any parts that contained substitute text (e.g.domain.tld
) are replaced with the correct text. - If the file contains blocked commands, remove those lines by placing a
#
character at the beginning of the line.
.htaccess Restore Failed
Issue: After restoring the default .htaccess, the website still behaves the same.
Cause: Either you are restoring the wrong .htaccess or the broken page is cached.
Solution: Refresh the page without the browser cache with the keyboard shortcut Ctrl+Shift+R.
To restore the default state of the .htaccess file somewhere other than the www folder (or the .htaccess of a content management system such as WordPress), retrieve it from a backup or content management website.
FAQ
Question: How do I restore a content management system (WordPress) .htaccess?
Answer: Before you start editing a .htaccess file, make a backup of it. If you don’t have it, either reinstall the content management system or look for the default .htaccess content of the same CMS version elsewhere. If all else fails, contact customer support.