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 http://mydomain.com/folder1/ will be automatically redirected to http://mydomain.com/folder2/.

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

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

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

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

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

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

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

To redirect requests with secure https prefixes (so that requests to http://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.

Was this article helpful?

Related Articles

Leave A Comment?