Development and databases
The .htaccess file, what you need to know
The .htaccess file lets you customise certain web server options in a directory or directory tree. The main ones are:
- Customise error pages
- Configure a directory's file listing
- Add mod_rewrite rules
- Deny access to certain IPs
- Add MIME types
- Set up redirects
- Change PHP options when the handler allows it
Among many other possibilities.
The .htaccess file works recursively: if you have a .htaccess file inside public_html with some customisations, they can also affect its subdirectories. The exact behaviour depends on the directive and on the rules in the lower directories; a second .htaccess does not necessarily override all of the first one.
Any directory can use a .htaccess file. Just create a file named .htaccess (the leading dot matters) via FTP or the cPanel File Manager, and edit it as you would a plain text file.
Find and edit it
- Confirm the document root of the domain or folder you want to configure; not every domain in an account uses
public_htmldirectly. - In the File Manager, turn on showing hidden files. The name starts with a dot and must not end in
.txt. - If it already exists, download a private copy before editing it. Do not replace all the content with an example that contains only one rule.
- Add your change outside the blocks the application regenerates, unless its documentation says otherwise.
- Save, test the affected URL and also check a page that should not change.
The server must allow the directive in that context. A rule that is valid in Apache's main configuration may not be allowed in .htaccess. At Hostsuar, LiteSpeed interprets compatible rules; use instructions suited to the service.
If an error appears after editing
Restore the previous content and check that the site recovers. Look at the log to find syntax errors or unsupported directives. Do not change permissions to 777 or delete all the rules to fix a 500 error.
For a redirect, also check the destination, the HTTP code and whether a loop forms. The browser or a cache may keep a previous redirect; test without that cache before concluding the rule does not work.
See the guides on mod_rewrite and the maintenance page for specific tasks.
You may also find useful
- Apache handlers Set from cPanel which Apache handler should process a given extension, and learn why this option cal...
- Set up friendly URLs and mod_rewrite rules Enable mod_rewrite from .htaccess and apply the rules each application provides to rewrite addresses...
- Available binaries and tools - FFmpeg, Tesseract, ImageMagick, cURL and more See which binaries are available for video, images, PDF, OCR, networking, DNS, version control and o...
- Create and run cron jobs in cPanel Set up cron jobs in cPanel, define intervals and commands, manage notifications, call URLs with cURL...
Something wrong or out of date? Tell us and we will review it.