Protect a file with a Password
In some cases, you might need to password protect a certain file(s). You can do just that by following the instructions below.
[content_control]
- Create a file named .htpasswd and place it above your root directory, so visitors can’t access it. Example of the root directory pathway: /home/cpanelusername/.htpasswd Create an encrypted password for the file.
- Right-click on the .htpasswd file and select the Code Edit option.
- In the popup that appears, click the Edit button.
- Place the generated .htpasswd information into the .htpasswd file.
- Click on the Save Changes button.
- Click on the Close button.
- Navigate to the directory where the file(s) that you would like to password protect lie.
- If an .htaccess file does not exist in the directory already, create one. (double-check your settings in the right corner that hidden files are on!)
- Right click on the .htaccess file and select the Code Edit option.
10. Insert the following code into the .htaccess file:
<FilesMatch “examplefile.extension”>
AuthName “Member Only”
AuthType Basic
AuthUserFile /home/cpanelusername/.htpasswd
require valid-user
</FilesMatch>
11. Be sure to replace example file.extension with your file’s actual name and replace cpanelusername with your actual cPanel username.
12. Click on the Save Changes button.
13. Click on the Close button.
For multiple files:
<FilesMatch “*.html”>
<FilesMatch “file.*”>
<FilesMatch “*.*”>
If you’re using WordPress and your installation is in your public_html folder, with permalinks, you’ll run into some issues. Below you’ll find the process to fix this.
NOTE: you should always have a website backup in case of a mishap
1. Edit the .htaccess file in your WordPress home folder (public_html) and remove the line of code shown in bold below:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
2. Replace that line with this new line:
RewriteRule ./ /index.php [L]
3. The final code snippet will look like this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ./ /index.php [L]
</IfModule>
# END WordPress
4. Save your changes
Now here’s for Resetting your Permalinks if needed.
- From the left-hand navigation menu in WordPress, click Settings > Permalinks. Note the current setting.
- Change the settings back to the previous configuration.
- Click > Save Settings.
[/content_control]
0 Comments
You must log in to post a comment.