Set up .htaccess: redirects, security and more
Published on July 10, 2026 10 min read
Learn what a .htaccess file is and how to set up redirects and security in Plesk, with ready-to-use code examples for LiteSpeed and Apache.
An .htaccess file is a small configuration file that lets you fine-tune the web server per folder, without touching the main server settings. You use it for common tasks, from redirects and security to custom error pages and caching. This guide explains what .htaccess is, where to find it in Plesk and how to edit it safely, with ready-to-use examples for redirects, security and more.
What is an .htaccess file?
The name stands for hypertext access. It is a plain text file that you place in a folder of your website, and that the web server reads before it serves a page. The settings apply to that folder and every folder below it. This lets you change how your site behaves without any programming and without access to the central server configuration.
.htaccess originally comes from the Apache web server. Our hosting servers run LiteSpeed or Apache, and both read .htaccess, so you can use the file on your hosting package. Whether your site runs on LiteSpeed shows in your browser's developer tools (F12, Network tab): the page itself then shows server: LiteSpeed. If a rule does not behave as expected, it can be down to the server configuration. In that case, get in touch with support.
What you can do: .htaccess redirects, security and more
An .htaccess file is surprisingly versatile. The most common uses fall into three groups:
- Redirects: send visitors and search engines on automatically, for example from HTTP to HTTPS or from an old URL to a new one.
- Security: shield folders, block IP addresses or protect a folder with a password.
- Server settings: show custom error pages, set up caching and fine-tune how your site works.
Below you will find a working example for each, which you can copy and adapt.
Where do you find .htaccess in Plesk?
The .htaccess file belongs in httpdocs, the main folder (document root) of your website. If your site runs on WordPress, there is usually already an .htaccess with the WordPress rules. If the file does not exist yet, you create it yourself.
This is how you open the file in Plesk:
- Open Plesk from the customer portal. How that works is explained in open Plesk from the customer portal.
- Choose Files in the menu on the left and open the
httpdocsfolder. - Is there no .htaccess yet? Then choose Create File, enter exactly
.htaccessunder File name (with the leading dot and no extension) and leave Use the HTML template off. If Plesk reports that a file with that name already exists, .htaccess is already there. - Open .htaccess with Edit in Code Editor or Edit in Text Editor in the menu next to the file, paste your rules and save with Save or OK.
Prefer an FTP program? You reach the same file over FTP or FTPS in the httpdocs folder. See uploading files with FTP, FTPS and SFTP. Turn on the display of hidden files in your FTP program as well, because files whose name starts with a dot are often hidden there.
The permissions in Plesk differ per hosting package. If you do not see an option from this article, create a support ticket and we will help you further.
Edit .htaccess safely: make a backup first
A small mistake in .htaccess can take your whole site down with a 500 error (Internal Server Error). So work carefully. First make a copy of the existing file, for example .htaccess-backup, or make a full backup of your website.
Add rules one at a time where possible and test after each change that your site still works. If you get a 500 error, remove the last rule or restore your backup. As soon as you save the file, your site works again. The error log shows which rule causes the error; read how to open it in viewing error logs in Plesk.
If you use WordPress, put your own rules above the line # BEGIN WordPress. WordPress can overwrite everything between # BEGIN WordPress and # END WordPress itself, for example when you save the permalinks.
Setting up redirects with .htaccess
Redirects send visitors and search engines on to a different address automatically. For SEO you almost always use a 301 (moved permanently), so the value of the old URL carries over.
1. Force HTTP to HTTPS
At LJPc this is usually already taken care of for you. LJPc sets up the SSL certificate automatically with Let's Encrypt once your domain points to the hosting, and the redirect from http to https is on by default in Plesk: the option Permanent SEO-safe 301 redirect from HTTP to HTTPS under Hosting Settings of your domain. Test it by opening your domain with http:// in front: if you end up on the https version, it works. You can read more about certificates in what an SSL certificate is and how to install it and in Let's Encrypt: set up your free SSL certificate.
If the option is off for your site, switch it on in Plesk or use these rules in .htaccess:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Do you get warnings about insecure content (mixed content) after moving to https? Then read fix mixed content and HTTPS errors.
2. www to non-www (or the other way around)
Pick one fixed spelling of your domain, so you do not end up with two versions side by side. You can do this without code in Plesk: under Hosting Settings, choose under Preferred domain whether your domain with or without www is preferred. Plesk then redirects the other spelling with a 301.
Would you rather handle it in .htaccess? Then use these rules. From www to without www:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]
Would you rather always use www? Then turn it around:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Use one of the two, not both at once, and handle it in one place: Preferred domain in Plesk or the rules in .htaccess. Two settings that contradict each other cause an endless redirect. If you combine this with the https rule above, put RewriteEngine On once at the top and the rules neatly underneath each other.
3. Redirect an old URL to a new one with a 301
Have you moved or renamed a page? Send the old address on to the new one:
Redirect 301 /old-page.html https://www.yourdomain.com/new-page
The first part is the path on your own site (start with a slash), the second part is the full new URL. To redirect a whole folder with a pattern, use RedirectMatch or a RewriteRule. It is best not to mix Redirect and RewriteRule in the same file, as they can clash.
Want to redirect a whole domain to another website, with the forwarding package, a domain alias or .htaccess? See how to redirect a domain to another website.
Handling security with .htaccess
A few rules make your site much harder to abuse. The examples below are a good starting point.
4. Turn off directory listing
If a folder has no index file, a web server can show a list of every file in that folder. On our servers, visitors do not get to see that list by default. If you want to lay it down for your site yourself, for example because your site may move later, turn the list off with:
Options -Indexes
Visitors then get an error message instead of an overview of your files.
5. Block IP addresses
Want to keep out a nuisance IP address? Then use the Apache 2.4 Require syntax:
<RequireAll>
Require all granted
Require not ip 203.0.113.5
Require not ip 198.51.100.0/24
</RequireAll>
The first line grants everyone access, the following lines exclude specific addresses or a whole range. Online you will also still see the old style with Order and Deny from. It is outdated; in any case, do not mix it with Require in the same file.
Then test whether the block works: temporarily add your own IP address to the list and open your site. If you get a 403 error (Forbidden), it works. Plesk stays reachable, so you remove your own address there again. Can you still see your site? Then create a support ticket and we will look into it with you.
6. Protect a folder with a password
You can shield a folder with a username and password. You do this with two files: an .htaccess with the settings and a .htpasswd with the encrypted login details.
AuthType Basic
AuthName "Protected area"
AuthUserFile /var/www/vhosts/yourdomain.com/.htpasswd
Require valid-user
The path to .htpasswd is an absolute path on the server and differs per account. Place the file outside httpdocs, as in the example, so nobody can request it through your website. The password in .htpasswd has to be encrypted; a password in plain text does not work. Easier is the built-in feature in Plesk: Password-Protected Directories for your domain. There you point to a folder and add a user with a password. Plesk then handles the settings and the password file for you. For step-by-step instructions, see how to password protect a directory or website in Plesk.
More server settings with .htaccess
Besides redirects and security, .htaccess also lets you steer error pages, caching and more.
7. Custom error pages (404 and 403)
Default error pages look bare. You may prefer to show visitors your own page that matches your site:
ErrorDocument 404 /404.html
ErrorDocument 403 /403.html
A 404 means a page does not exist, a 403 that access is denied. Create the files 404.html and 403.html yourself in your main folder. The path starts with a slash, counted from the document root.
8. Set caching headers
With caching you let a visitor's browser store images, CSS and JavaScript for a while, so that later visits load faster. With the mod_expires module you set a retention time per file type:
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
</IfModule>
Whether these rules are applied to your site depends on the web server. So check it in your browser's developer tools: choose the Network tab, reload the page and click an image. If the response headers show cache-control with a max-age, it works. If you see no max-age, get in touch with support. Caching is one of the ways to make your site faster; you will find more tips and a more extensive example in speeding up your website.
9. Change PHP settings
Online you often see PHP lines like php_value upload_max_filesize 64M in a .htaccess. Such a line does not work on every server: on our servers with Apache it causes a 500 error, because PHP does not run as an Apache module there.
So change PHP settings under PHP Settings of your domain in Plesk. There you set upload_max_filesize, post_max_size and memory_limit, among others, and that works on all our servers. You can read how to change those settings in changing PHP settings in Plesk. If the setting you are looking for is not there, create a support ticket.
Security headers: do it in the right place
With mod_headers you can also set security headers, such as X-Content-Type-Options or a Content-Security-Policy. That works fine in .htaccess. For HSTS (Strict-Transport-Security), first check whether it is already sent: when our system requests the SSL certificate automatically, it switches HSTS on straight away. Look at the headers of your page in the Network tab, or use the command curl -I https://yourdomain.com. If Strict-Transport-Security is already there, do not add it a second time, because a duplicate header can cause problems. If it is missing, switch HSTS on in Plesk. How that works and which other headers you can set is explained in setting up HTTP security headers. CORS headers, which decide whether another website may load your fonts or data, are also set with mod_headers; see what CORS is and how to fix CORS errors.
Still stuck?
Unsure about a rule, or does your site keep showing an error? First restore your backup, so your site works again. Then create a support ticket, call us on +31 85 130 6429 or email support@ljpc.nl. We are happy to think along with you.
Frequently asked questions
Does .htaccess work on my LJPc hosting?
Yes. Our hosting servers run LiteSpeed or Apache, and both read .htaccess. You place the file in the httpdocs folder and the settings work right away. The effect of some rules, such as caching, can differ per server, so check after a change that it works.
Why do I get a 500 error after editing .htaccess?
A 500 error (Internal Server Error) is almost always caused by a typo or a rule the server does not support. Remove the last rule you added or restore your backup, and your site works again straight away. A common cause is a php_value line; that is why you change PHP settings under PHP Settings in Plesk.
Where exactly is the .htaccess file?
In httpdocs, the main folder of your website. In Plesk you open that folder through Files. In an FTP program, turn on the display of hidden files, because files whose name starts with a dot are often hidden there.
Can I change PHP settings through .htaccess?
Use PHP Settings in Plesk for that. A php_value line in .htaccess does not work on every server and causes a 500 error on our servers with Apache. Through PHP Settings it works on all our servers.
Do I have to set up a redirect from HTTP to HTTPS myself?
Usually not. At LJPc the option Permanent SEO-safe 301 redirect from HTTP to HTTPS under Hosting Settings in Plesk is on by default. Open your domain with http:// in front to test it. If the option is off, switch it on in Plesk or use the RewriteRule from this article.
Do I have to add an HSTS header myself?
First check whether it is already there, with curl -I https://yourdomain.com or in the Network tab of your browser. When our system requests the certificate automatically, HSTS is already on and you do not add it again, because a duplicate header can cause problems. If it is missing, switch HSTS on in Plesk under SSL/TLS Certificates.