.htaccess is a simple text-based configuration file that allows you to modify the behaviour of the Apache web server on a directory-by-directory basis, without needing to access the server’s central settings. The web server reads it with every request, so changes take effect immediately; no restart is required.
Where is it based?
Our web hosting plans run on the Apache web server, and .htaccess rules can also be used on cPanel hosting. On static hosting, however, files are served by Nginx, which does not read the .htaccess file; therefore, the rules contained therein have no effect.
A file located in the website’s root directory (the domain’s root directory) applies to the entire website, whilst one placed in a subdirectory applies only to that directory and its subdirectories. An .htaccess file in a subdirectory may override the rules defined at a higher level.
What can it be used for?
- Redirects: redirecting old URLs to new ones, from
http://tohttps://, from a non-wwwaddress to awwwaddress, or vice versa. - URL rewriting: WordPress and many other content management systems use
mod_rewriterules to generate ‘clean’, parameter-free URLs. - Access restrictions: password-protected directories, IP address blocking, and blocking of certain file types.
- Custom error pages: for example, displaying a bespoke 404 page.
- Caching and compression: configuring the cache headers sent to the browser, which speeds up page loading for returning visitors.
Some common examples
Permanent (301) redirect for an old page:
Redirect 301 /regi-oldal.html /uj-oldal/
Disabling the directory listing so that the contents of a folder without an index_file cannot be browsed:
Options -Indexes
Custom 404 error page:
ErrorDocument 404 /404.html
How to edit this?
- Connect to your hosting account via FTP, or open the file manager in the control panel.
- As the file name begins with a full stop, many programs treat it as a hidden file: enable the display of hidden files.
- Before making any changes, create a backup of the existing file so that you can restore it in the event of an error.
- Save the file using a plain text editor with UTF-8 encoding; its name must be exactly
.htaccess, without the.txtextension. - After saving, open the website and try out the modified URLs as well.
Common errors
- 500 Internal Server Error: the entire page becomes inaccessible due to a typo or a command that is not permitted on the server. Restore the backed-up file or delete the lines you last entered. In the case of WordPress, resetting the .htaccess file to its default state may also help.
- PHP settings: the
php_valueandphp_flaglines cause errors when using PHP-FPM. Please configure the PHP parameters in the .user.ini file. - Redirect loop: if two rules redirect to each other (or if a Cloudflare setting conflicts with HTTPS enforcement), the browser will display a ‘too many redirects’ error.
- Overridden WordPress block: the section between
# BEGIN WordPressand# END WordPressis handled by WordPress itself; write your own rules outside this block, before it.
You can find redirect examples on our blog: .htaccess redirects: a practical guide to setting them up correctly.