How to Manually Redirect Domains via (dot)htaccess in cPanel Make it Rain Paper

  • Domain Redirection, htaccess
  • 0

Domain redirection is a common task for website administrators. While cPanel provides an interface for adding redirects, sometimes these do not work as expected due to other redirects or rewrites already present in the (dot)htaccess file. This guide explains how to manually add domain redirects using (dot)htaccess rules to ensure they take effect properly.

Pre-requirements

  • Access to your cPanel account or SSH (Shell Access) to the server.
  • Basic knowledge of editing files via SSH or cPanel File Manager.
  • Familiarity with (dot)htaccess syntax and regular expressions (regex).

Steps to Manually Add a Domain Redirect via (dot)htaccess

  1. Log in to your cPanel account or access your server via SSH.
  2. Open the File Manager in cPanel or use a text editor like vi, vim, or nano in SSH to edit the (dot)htaccess file.
  3. Locate the (dot)htaccess file in the root directory of your domain (usually /public_html/).
  4. Add the following syntax to redirect all requests from example_domain.com and www.example_domain.com to https://www.another-example_domain.com/test-page/:

RewriteCond %{HTTP_HOST} ^example_domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example_domain\.com$
RewriteRule ^(.*)$ "https\:\/\/www\.another-example_domain\.com\/test\-page$1" [R=301,L]

Explanation of the Code

  • RewriteCond %{HTTP_HOST}: Checks if the request is coming from the specified domain (example_domain.com or www.example_domain.com).
  • RewriteRule: Redirects all traffic to the target page at another domain. The [R=301,L] flags indicate a permanent redirect (301) and tell the server to stop processing other rules (L).

Important Notes

  • WordPress and CMS Considerations: If your website is powered by a CMS like WordPress, ensure this redirect is placed before any other CMS rewrites in the (dot)htaccess file. Failure to do so may result in the CMS overriding the redirect.
  • The cPanel interface for redirects appends to the (dot)htaccess file, but it doesn't manage conflicts with existing rules. Manual editing is often required for advanced redirects.
  • Consult a system administrator if you face issues with redirect configurations beyond the scope of cPanel's built-in tools.

Best Practices for (dot)htaccess Redirects

  • Place redirects at the top of the (dot)htaccess file to ensure they are processed first.
  • Test your redirects using tools like HTTP Status Checker to verify they are functioning as intended.
  • Be cautious with regular expressions (regex) in your redirects, as mistakes can lead to unexpected results or redirect loops.

Manually adding redirects via (dot)htaccess allows for greater control over how domain traffic is handled, especially in complex scenarios where cPanel's interface may not suffice. By following the steps above, you can easily implement redirects and resolve issues caused by conflicting rules in the (dot)htaccess file.

For additional help, please contact our hosting support team.


Did this answer help?

« Back