How to use .htaccess rewrite rules

.htaccess is a configuration file used by the Apache web server. .htaccess rules override global settings for the directory in which the file is placed. You may find that .htaccess files are created automatically on your server, when you install popular web applications like WordPress, Drupal, and Magento. However, if one does not exist, it can be easily created in a text editor and uploaded to your server. You can also create one directly from cPanel’s File Manager.

To get started, log in to your HostPapa Dashboard. In the top navigation menu, select My cPanel.

Now select File Manager.

cPanel

At the top right of the screen, click the Settings button.

cPanel File Manager Settings

.htaccess files are hidden from view by default. To expose them, ensure Show Hidden Files (dotfiles) is checked in the Preferences panel, then click Save.

Show Hidden Files option

Navigate to your website root using the left sidebar. Check to see whether a .htaccess file currently exists.

.htaccess file listing

If the .htaccess file exists, highlight it and select Edit from the top menu.

File Editing in cPanel

Alternatively, you can create a new .htaccess file by selecting + File.

Creating a New File

In the New File panel that opens, enter .htaccess as the file name and select Create New File to open the cPanel text editor. If you are asked to confirm Encoding settings, leave the default option in place and click Edit.

You can now create the rules required for your site. Here are some examples to try.

Redirecting requests

.htaccess rewrite rules can be used to direct requests for one subdirectory to a different location, such as an alternative subdirectory or even the domain root. In this example, requests to https://mydomain.com/folder1/ will be automatically redirected to https://mydomain.com/folder2/.

Options +FollowSymLinks
 RewriteEngine On
 RewriteRule ^folder1.*$ https://mydomain.com/folder2/ [R=301,L]

To redirect traffic to the domain root, use the following rule:

Options +FollowSymLinks
 RewriteEngine On
 RewriteRule ^folder1.*$ https://mydomain.com/ [R=301,L]

To redirect requests to subdirectory (i.e. so that requests for https://mydomain.com/foo.html are directed to https://mydomain.com/folder1/foo.html) use the following rule:

Options +FollowSymLinks
 RewriteEngine On
 RewriteCond %{HTTP_HOST} example.com$ [NC]
 RewriteCond %{HTTP_HOST} !folder1
 RewriteRule ^(.*)$ https://mydomain.com/folder1/$1 [R=301,L]

If you wish to redirect requests without a www prefix (so that requests to https://mydomain.com/foo.html are directed to https://www.mydomain.com/foo.html) use the following rule:

Options +FollowSymLinks
 RewriteEngine on
 RewriteCond %{HTTP_HOST} ^example.com [NC]
 RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]

To redirect requests with secure https prefixes (so that requests to https://mydomain.com/foo.html become https://mydomain.com/foo.html) use the following rule:

RewriteEngine On
 RewriteCond %{SERVER_PORT} 80
 RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

For further questions, or if you need help, please open a support ticket from your HostPapa Dashboard. Follow this link to learn how.

Related Articles

Get online with our affordable web hosting

Get online with our affordable web hosting

Learn more now
HostPapa Mustache