Redirects basically allow you to change the accessibility of a web page through different URLs, and can serve several purposes
Managing this behavior can be done in a couple of ways:
1. Managing Redirects through the .htaccess file
You can edit the file through cPanel >> File Manager.
There is a Settings button (top right) >> Preferences >> check the "Show Hidden Files (dotfiles)" box, then click Save.
The .htaccess file (a configuration file used by the Apache web server to control various aspects of your website’s behavior) can be found in /public_html folder.
The first step would be to load the `mod_rewrite` module in Apache, then add all the rules under RewriteEngine On.
<IfModule mod_rewrite.c>
RewriteEngine On
</IfModule>
Some common useful redirects include:
Redirect a single URL:
Redirect 301 /old_page/ https://www.example.com/new_page/
Redirect an entire website
Redirect 301 / https://www.example.com/
Redirect all old URLs to the new URL:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule (.*)$ https://www.new_example.com/$1 [R=301,L]
</IfModule>
*this shows how the old "example.com" would redirect while preserving URLs to "new_example.com"
Redirecting to a local or an external site file
Redirect /path/old_file.html /path/new_file.html
Redirect /path/old_file.html https://www.example.com/new_file.html
Error message redirection
ErrorDocument 404 https://example.com
/
*useful to redirect 404s for example, to the homepage of the website
Rewriting non-existing links to index.php
The following will redirect all links to files or directories that do not exist to index.php. However, if the file or directory does exist, it will be loaded normally:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Editing the .htaccess file directly can be fast and efficient, but small errors here can result in misconfiguration or site downtime, so it's highly recommended that you back up the file before making any changes (this can be done by always having a copy of .htaccess stored before attempting to make changes). It's also a good practice to use developer tools to test that redirects work as intended.
Finally, there is the option to edit the .htaccess file redirects, to for your website to use HTTPS:
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [OR,NC]
RewriteCond %{https} off
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
2. Using cPanel’s (Webspace) Redirects Interface
Access cPanel: Log in to your hosting account and navigate to the cPanel dashboard.
Find the Redirects Section: Look for the “Redirects” icon, typically located under the “Domains” section.
Configure the Redirect:
Type: Choose between a temporary (302) or permanent (301) redirect.
URL: Specify the URL to redirect from and the destination URL.
Wildcard Option: Enable this option if you need to redirect multiple pages with similar URL patterns.
Apply the Changes: Click the “Add” button to save your redirect settings.
Using cPanel interface to manage Redirects has the advantage of using the graphical interface which minimizes the risk of syntax errors and it's the ideal option for users who need to implement simple redirects quickly.
Keep in mind that it might be neccesary to clear your browser’s cache to see the updated redirect behavior.