How to redirect a page to another page or website using .htaccess?
This guide walks you through redirecting pages using .htaccess rules in cPanel. You'll learn the difference between Redirect and RewriteRule, how to test safely with 302 status codes, and how to troubleshoot common issues like redirect loops.
Edit your site’s .htaccess in cPanel and add a Redirect or RedirectMatch rule. Test with 302 first, then switch to 301 when confirmed.
Before you start
- Log in to your Hostworld cPanel account and know which folder is the document root for the site you are editing.
- Have the exact old URL path and the new destination URL ready.
- Download a copy of your current .htaccess so you can undo any change.
Step 1: Open .htaccess in cPanel File Manager
In cPanel, open File Manager. Click Settings in the top right and enable “Show Hidden Files (dotfiles)”. Locate .htaccess in your site’s document root and Edit it. If you use cPanel’s Redirects tool instead, know that it appends rules to the bottom of .htaccess and you may need to move them higher later.
Step 2: Add a basic redirect with mod_alias
Use Redirect for one-to-one moves and RedirectMatch for simple patterns. Redirect preserves the query string automatically and turns POSTs into GETs.
# One page to another page or site, test as 302 first
Redirect 302 "/old-page" "https://www.example.com/new-page"
# Pattern match and preserve the rest of the path
RedirectMatch 302 "^/old/(.*)$" "https://www.example.com/new/$1"
Notes: status comes first, then the URL-path starting with a slash, then the destination. Omit “302” only if you want a temporary redirect by default; you will make it permanent later.
Step 3: Control the query string or add conditions with mod_rewrite
Use mod_rewrite only when you need conditions or query-string changes. In .htaccess, RewriteRule patterns do not start with a slash.
# Drop the original query string
RewriteEngine On
RewriteRule ^old-page$ https://www.example.com/new-page [R=302,L,QSD]
# Add your own query string and keep the original too
RewriteEngine On
RewriteRule ^promo$ https://www.example.com/new-promo?src=site [R=302,L,QSA]
Step 4: Save and test with 302
Save the file and test in a new private browser window. 301 responses are heuristically cacheable, so confirm behaviour with 302 first to avoid caching a mistake.
Step 5: Make it permanent and know how to undo
When you are happy, change 302 to 301 on your Redirect, RedirectMatch or RewriteRule. If you used cPanel’s Redirects interface and the rule sits below large CMS blocks, move your redirect lines higher so they are not overridden. To undo, remove the line you added or comment it out with a leading #, then save the file and retest.
If it does not work
- 500 error after saving: a syntax error or an unpermitted directive in .htaccess will fail the request. Reopen File Manager, remove the last change or restore your backup, then save. If errors persist, open a support ticket and we will check the server error log.
- Redirect loop, especially on HTTP to HTTPS: add a condition to avoid redirecting when already on HTTPS.
RewriteEngine On RewriteCond "%{HTTPS}" !=on RewriteRule "^(.*)" "https://%{SERVER_NAME}$1" [R=301,L] - Rule never matches: for RewriteRule in .htaccess do not use a leading slash in the pattern. For Redirect and RedirectMatch include the leading slash. Avoid over‑broad patterns like
.*that can hijack assets or admin paths.
What next
If you want the full background, including more RedirectMatch examples and ordering tips, read our guide on redirects in .htaccess in the Guides section. Running this on a VPS instead of cPanel shared hosting? See our VPS guides. If you are planning to host this on your own server, our Linux VPS page explains the UK location and options.
Common questions
Should I use 301 or 302 for a redirect?
Test with 302 first to avoid caching errors, then switch to 301 once you have confirmed the redirect works as intended. Use 301 for permanent redirects and 302 for temporary ones.
What is the difference between Redirect and RewriteRule?
Redirect and RedirectMatch are simpler for one-to-one page moves and automatically preserve query strings. RewriteRule is needed when you require conditions, pattern matching, or need to control or drop query strings.
Why am I getting a 500 error after adding a redirect?
A syntax error or unpermitted directive in .htaccess causes a 500 error. Remove or revert your change, restore a backup, and check the server error log for details.
How do I avoid a redirect loop?
Add a condition to check the current state before redirecting. For HTTP to HTTPS redirects, use RewriteCond to ensure you do not redirect when already on HTTPS.
Why does my RewriteRule pattern not match?
In .htaccess, RewriteRule patterns must not start with a slash, but Redirect and RedirectMatch patterns must. Check your syntax against the guide.
Was this article helpful?
0 people found this helpful