
How to Set Up Redirects and Rewrite Rules in cPanel
How to Set Up Redirects and Rewrite Rules in cPanel
A comprehensive guide to creating 301/302 redirects and rewriting URLs for better SEO and site management.
Why Redirects and Rewrite Rules Matter
Redirects and URL rewrites are essential tools for website owners and developers. Whether you’re moving pages, restructuring your site, or optimizing URLs for search engines, setting up proper redirects and rewrite rules can:
- Preserve your search engine rankings
- Avoid broken links and 404 errors
- Improve user experience
- Enable temporary or permanent URL changes
In this guide, you will learn how to create 301 (permanent) and 302 (temporary) redirects and how to use rewrite rules to manage URLs effectively using cPanel.
Understanding Redirect Types
- 301 Redirect (Permanent): Tells browsers and search engines that a page has permanently moved to a new URL. SEO benefits are transferred.
- 302 Redirect (Temporary): Indicates the move is temporary, so search engines keep the original URL indexed.
- Rewrite Rules: Used to change how URLs appear without changing the underlying file structure, typically via
.htaccess
files.
Step 1: Creating Redirects in cPanel
Access the Redirects Tool
- Log in to your cPanel account.
- Scroll down to the Domains section.
- Click on Redirects.
Set Up a Redirect
- Choose Redirect Type: Select Permanent (301) or Temporary (302) from the dropdown.
- Select Domain: Choose the domain you want to redirect from the dropdown.
- Enter the Redirect Path: Specify the exact page or leave blank to redirect the entire domain.
- Destination URL: Enter the full URL where you want to send visitors (e.g.,
https://newdomain.com/page
). - Options:
- Redirect with or without www: Choose whether to redirect both www and non-www versions.
- Wild Card Redirect: Check this box if you want to redirect all files within a directory to the same files in the new location.
- Add Redirect: Click the Add button to save the redirect.
Step 2: Managing Redirects
- View existing redirects on the same page.
- Remove or edit redirects as needed to maintain proper site navigation.
Step 3: Using Rewrite Rules via .htaccess in cPanel
What is .htaccess?
The .htaccess
file is a powerful configuration file used by Apache servers to control website behavior, including URL rewriting.
Access .htaccess File
- In cPanel, open File Manager.
- Navigate to the root directory (
public_html
or the relevant domain folder). - Enable Show Hidden Files (dotfiles) from settings to see
.htaccess
. - Edit the
.htaccess
file or create one if it doesn’t exist.
Example Rewrite Rules
Redirect non-www to www:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Rewrite URLs to remove .php
extension:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Redirect HTTP to HTTPS:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]
Best Practices for Redirects and Rewrite Rules
- Test Redirects: Use online tools to check if redirects are working properly.
- Avoid Redirect Chains: Too many redirects can slow down your site and harm SEO.
- Backup .htaccess: Always back up your
.htaccess
file before making changes. - Use Clear URLs: Rewrite rules should make URLs clean and user-friendly.
Frequently Asked Questions (FAQs)
- How long does a 301 redirect take to update in search engines?
- It can take days or weeks; 301 redirects inform search engines permanently but require crawling.
- Can I set up redirects without affecting SEO?
- Yes, if done correctly—301 redirects transfer SEO value; 302 should be used only temporarily.
- What happens if I remove a redirect?
- Visitors may see 404 errors unless the original page is restored or redirected elsewhere.
Need Assistance with Redirects?
If you host with Hiverift, their expert support can help you configure redirects and rewrite rules:
- Visit https://hiverift.com
- Use live chat or submit a ticket from your client area