How to Create a 301 Redirect in .htaccess
Create a 301 redirect in a document-root .htaccess file with RewriteEngine On and an anchored RewriteRule such as ^old-page/?$ /new-page/ [R=301,L]. Put custom rules above WordPress's managed block, preserve a working backup and test the status and Location header with curl. Domain moves also need hostname conditions and functioning DNS and HTTPS.
For Apache site owners moving pages or domains, with exact-match patterns, proxy cautions and checks that distinguish correct redirects from cached mistakes.
knowledge
- Read time
- 5 min
- Sections
- 7
- Updated
- 14 Nov 2026
- Written by
- WebJi® team
Last editorial update: 14 November 2026.
What does a .htaccess 301 redirect change?
A .htaccess 301 redirect returns a permanent-move response with a Location header telling the client which URL to request next. It changes the address visitors follow; it does not copy files, create the destination page or update links inside your content.
Apache reads per-directory configuration only when the relevant overrides are allowed. On a typical hosting account the document-root file is in public_html, but addon domains may have another root. Nginx does not process .htaccess. Confirm the server and document root before pasting Apache directives.
The .htaccess editing guide covers locating and backing up this hidden file. Keep a known-good copy outside public access, preserve access-control rules and test on a staging host where possible. Browsers may cache a permanent redirect, making an incorrect 301 harder to unwind.
- 301: use for a confirmed permanent move.
- 302: a temporary redirect useful during a controlled test, not proof the final 301 is correct.
- Internal rewrite: serves a different resource without redirecting the visitor's address; it is not a 301.
How do you redirect exactly one page in .htaccess?
Use an anchored RewriteRule for an exact old page, so similar prefixes and child paths do not redirect accidentally. In a document-root .htaccess file, the rule pattern does not start with a slash.
RewriteEngine On
RewriteRule ^old-page/?$ /new-page/ [R=301,L]
This example matches /old-page and /old-page/, but not /old-page/child or /old-page-extra. It assumes the new path exists and is not itself redirected. Put the custom rule above the WordPress-managed block, not inside it.
Apache's simple Redirect 301 /old-page/ /new-page/ directive is a prefix redirect: matching path suffixes are appended to the destination. That may suit a folder move, but it is not the same as an exact-match rule. Avoid mixing mod_alias and mod_rewrite casually on a WordPress site because module processing is not determined solely by the order of lines.
On cPanel hosting, the panel's Redirects interface can create rules, but inspect the generated result and test its scope. If a WordPress plugin owns the mapping, use the WordPress redirect guide rather than maintaining two competing versions.
How do you redirect an old domain while keeping each page path?
A domain redirect should match only the old hostname and send the requested path to a fixed new hostname. Use this document-root example only when equivalent pages exist at the same paths on the new domain.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$ [NC]
RewriteRule ^(.*)$ https://newdomain.com/$1 [R=301,L]
The condition prevents requests on newdomain.com from matching the old-host rule. The destination hostname is fixed, rather than built from an arbitrary Host header. Replace both example domains with domains you control, and configure HTTPS certificates for the old and new names.
An HTTPS request must complete TLS before the server can return the redirect. Consequently, an expired certificate on the old domain can block the move before the browser sees the 301. Keep the old domain registered, resolving and able to answer requests while old links remain relevant. The website domain-change guide covers the wider migration.
How do you combine HTTPS and www redirects in .htaccess?
Combine the HTTP and hostname conditions into one redirect to your chosen HTTPS address to avoid an unnecessary two-hop chain. The following example assumes Apache terminates TLS directly and the only accepted hostnames are example.com and www.example.com.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
An already canonical https://www.example.com/ request does not match. Both HTTP forms and HTTPS without www should move directly to the final HTTPS www URL. Choose non-www instead if that is the site's canonical address, and ensure the certificate covers every HTTPS hostname visitors use.
Behind a CDN or reverse proxy, Apache may see HTTP even when the visitor used HTTPS. Do not copy a blanket X-Forwarded-Proto rule that trusts any client-supplied header. Have the host configure trusted proxy handling or perform the redirect at the TLS edge, then test every public variant.
How do .htaccess redirects handle folders and query strings?
A folder redirect captures the suffix after the old folder; query strings are normally carried forward unless you explicitly discard or replace them. Add only the rule needed for the migration, not every example together.
RewriteEngine On
RewriteRule ^old-folder/(.*)$ /new-folder/$1 [R=301,L]
With no query string in the destination, /old-folder/report/?lang=en redirects to /new-folder/report/?lang=en. If those parameters should not survive, Apache 2.4 supports the explicit QSD flag:
RewriteRule ^old-folder/(.*)$ /new-folder/$1 [R=301,L,QSD]
The Apache RewriteRule flag reference documents R, L and QSD. R=301 selects the status; L ends the current rules pass. It does not prevent the browser making another request, so the new URL must not match a rule that sends it back.
Do not add a universal trailing-slash rule without excluding files and checking application routing. A rule that turns /image.jpg into /image.jpg/ can break assets. Map changes according to the destination's actual URL structure, not a cosmetic preference.
How do you test a 301 redirect and recover from mistakes?
Test the initial status and Location header, then follow the chain and confirm the destination returns usable content. A browser landing somewhere is insufficient: it may have reused a cached redirect.
curl -I https://example.com/old-page/
curl -IL --max-redirs 5 https://example.com/old-page/
The first command checks the response to a HEAD request without following it. The second follows redirects up to the chosen limit. Look for 301 at the source, the intended Location and an eventual successful destination. If the application handles HEAD differently, repeat using a GET while discarding the body:
curl -sS -D - -o /dev/null https://example.com/old-page/
| Result | Check |
|---|---|
| HTTP 500 after editing | Restore the known-good file and inspect the server error log for unsupported or malformed directives |
| Too many redirects | Check that host, scheme and path rules all point in one direction |
| Unrelated paths redirect too | Check anchors and whether a prefix directive was used unintentionally |
| Old behaviour remains | Check browser/CDN cache and the active document root |
Do not remove the entire .htaccess file as a generic recovery step: it may contain security restrictions and application routing. Restore the saved working configuration instead. Update internal links, sitemap entries and canonical URLs to the final destination after verification.
.htaccess redirect questions
Does a 301 redirect move database records or images?
No. A 301 tells clients to request another URL; content migration is separate. Copy and verify files, database references and media before activating the mapping, otherwise the redirect can correctly send readers to a page that still has missing images or application errors.
Why is a rule above WordPress's block recommended?
WordPress can regenerate the block between its BEGIN and END markers, so custom rules inside it may disappear. Placing a custom mod_rewrite redirect before the front-controller rules also lets the old path redirect before WordPress routes it into the application.
Can .htaccess redirect a domain with no working DNS?
No. Visitors must reach a server that reads the configuration before a redirect can be returned. Keep the old domain resolving to the redirect host and maintain its HTTPS certificate. A domain left expired cannot provide a reliable redirect through an old configuration file.
Should every removed page redirect to the homepage?
No. Redirect to a genuine replacement serving the same need. If no relevant replacement exists, review whether the page should return a normal not-found or gone response instead. A blanket homepage redirect can leave readers confused and does not recreate the removed content.
Last editorial update: 14 November 2026.
Ready to get started with WebJi®?
Join 12,000+ businesses across India and overseas. INR pricing, 18% GST shown before payment and 24/7 email support in Hindi and English — average first reply under 60 minutes.