To add a redirect in WordPress’s .htaccess, confirm the site runs Apache with mod_rewrite, back up the file, place a specific RewriteRule before the generated WordPress block, test it as a temporary redirect, and change it to R=301 only after the old and new URLs work without loops. If the server is Nginx, .htaccess is ignored and the equivalent rule belongs in the Nginx server-block configuration.
Antihost offers managed WordPress hosting with free migration, one-click staging and a 14-day free trial.
What .htaccess controls in WordPress
.htaccess is Apache’s distributed, per-directory configuration file. A file in the document root affects requests in that directory and its children; a file inside a WordPress subdirectory applies to that directory’s request path. It is not a universal WordPress settings file and it is not processed by Nginx.
As an Amazon Associate I earn from qualifying purchases.
On an Apache installation, WordPress uses the mod_rewrite extension to support “pretty” permalinks. The rewrite rules send requests that are not existing files or directories to index.php, where WordPress resolves the permalink. A redirect rule must therefore be placed and ordered so it runs before that broad front-controller rule consumes the request.
First identify the web server. Your host, control panel, server headers, or hosting documentation can confirm whether the site uses Apache, an Apache-compatible service, or Nginx. If it is Nginx, stop editing .htaccess; ask the host where its server-block redirect rules are managed instead. On Apache, verify that mod_rewrite is loaded. WordPress treats that module as the component required for URL rewriting, and the host must enable it when it is unavailable.
#1 Best Overall
Prepare safely before changing the file
- Make a copy. Download the current
.htaccessor duplicate it in the hosting file manager before changing anything. Keep the backup until every affected URL has been checked. - Locate the active file. Use the document root for a root installation, or the directory containing the WordPress installation for a subdirectory site. A file in a different directory may not affect the request you are testing.
- Confirm override support. Apache can be configured to ignore per-directory directives. If the file appears to have no effect, ask the host whether its directory and override policy permits
.htaccessrules. - Record the URL model. Note the WordPress Address, Site Address, hostname, HTTPS termination point, and trailing-slash convention. Redirects that cross domains, schemes, or directories can fail when these assumptions disagree.
- Plan a reversible test. Use a temporary redirect status while checking behavior, clear browser and caching layers, and test the old URL, its destination, and nearby paths before making the redirect permanent.
Restore or generate WordPress’s baseline rules
In the WordPress dashboard, open Settings > Permalinks and save the current permalink structure. This refreshes the rewrite configuration. The screen also displays the rules associated with that structure. When WordPress cannot write the file, copy the displayed rules and paste them into the correct .htaccess file manually.
For a root-installed site, WordPress documents this baseline pattern:
Windows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallOutdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware match# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
The !-f and !-d conditions leave existing files and directories alone. Other requests are handed to /index.php. A WordPress installation in a subdirectory can have a different generated base and destination, so use the rules shown for that installation rather than blindly copying the root example.
Do not remove or casually edit the # BEGIN WordPress and # END WordPress markers. WordPress may regenerate the content between them when permalinks are saved. Keep hand-written redirects and security directives outside the marked section whenever possible.
Place a custom redirect before the generated block
Apache evaluates rewrite rules in order. Put narrow, hand-written redirects above the WordPress markers so they can match the old request before the broad front-controller rule. A typical layout is:
# Custom redirects first
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^old-page/?$ https://example.com/new-page/ [R=301,L]
</IfModule>
# BEGIN WordPress
...generated WordPress rules...
# END WordPress
The pattern in an .htaccess per-directory context normally does not begin with a leading slash. In the example, ^old-page/?$ matches /old-page and /old-page/ at the site root, but not /old-page/extra. The destination can be an absolute URL or a path on the same host.
Free tools Windows power users keep installed
One-click scans. No signup required.
[R=301,L] has two jobs: R=301 sends a permanent redirect, and L stops processing further rules for that request. Use a temporary redirect while testing when you need to change the destination easily; browsers and intermediary caches can retain a 301, making mistakes harder to undo.
Understand per-directory matching and rule syntax
In a root .htaccess, Apache evaluates the requested path relative to that directory. That is why the pattern is usually written as ^old-page/?$, not ^/old-page/?$. In a WordPress installation under /blog, the active file and the generated RewriteBase differ, so confirm the path shown by Settings > Permalinks before adapting a rule.
The ^ anchor marks the beginning of the relative path and $ marks its end. Keep both anchors for a one-page redirect. A question mark after the slash, as in /?, permits either a trailing slash or no trailing slash. Literal regular-expression characters must be escaped: a dot in an old filename is written ., for example. Do not copy a pattern containing a leading slash from a server-level configuration into .htaccess without checking its context.
The L flag is particularly important when several rule sets are present. Without it, later rules can continue processing the request and create an unexpected chain or replace the response. Keep each migration rule narrowly scoped, and use separate rules when two old paths have different destinations rather than one expression that is difficult to audit.
Recommended Free Tools
Build precise redirect rules
Old slug to a new slug
For a root-installed site where both URLs stay on the same host:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^old-slug/?$ /new-slug/ [R=301,L]
</IfModule>
Replace both paths with the real slugs. Keep the anchors, because removing ^ or $ can allow the rule to match a larger set of URLs than intended. Escape regular-expression characters when they are literal characters in the old path; for example, a literal dot is written as ..
Rank #3
- Used Book in Good Condition
One old page to another hostname
RewriteRule ^old-page/?$ https://example.com/new-page/ [R=301,L]
Use the destination hostname and scheme that the site actually serves. Check that the destination does not redirect back to the old host or to a URL that can trigger the same rule.
Several old paths with a controlled pattern
A pattern is appropriate only when every URL it matches should receive the same destination logic. Make the expression as narrow as the migration requires, and test a valid old path, a similar but unrelated path, and the destination itself. A broad expression placed above WordPress’s rules can capture pages that were never part of the redirect plan.
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Scan for outdated or missing drivers - takes under a minute3Repair Windows errors before they cause bigger problemsHTTP to HTTPS
Use the HTTPS strategy documented by the host and test it behind any reverse proxy or load balancer. In a proxy setup, the proxy may terminate TLS while the origin connection remains HTTP. If WordPress believes every request is HTTP, an HTTPS redirect can repeat indefinitely. Confirm how the proxy communicates the original scheme and how WordPress is configured to trust it before enabling a host-wide rule.
Root URL to a subdirectory
Apache can match the site root and send it to a chosen subdirectory, but adapt the domain and directory names to the actual installation. Test both the bare host and requests below the root so that a root redirect does not unexpectedly capture the WordPress application or administrative paths.
Coordinate redirects with WordPress URL and site migrations
A domain, directory, or scheme migration is not completed by one redirect line. When the WordPress Address or Site Address changes, update the site’s URL settings and regenerate the rewrite rules after the new location is established. Retain the previous configuration backup so you can restore service if the new hostname, directory, or permalink base is wrong.
Check these variables together:
- the hostname used by the old and new URLs;
- whether the canonical scheme is HTTP or HTTPS;
- where TLS is terminated when a reverse proxy is present;
- whether WordPress is installed at the document root or a subdirectory;
- whether the site expects trailing slashes; and
- whether the destination can be requested without matching the old-path rule again.
Changing only one of these can produce a 404, a redirect chain, or a loop. After the migration, save Settings > Permalinks and confirm that the generated rules reflect the new location.
Use a migration runbook for larger changes
- Inventory old URLs. List the old hostname, directory, scheme, and permalink forms that need to remain reachable. Separate exact one-to-one moves from groups that genuinely share a pattern.
- Prepare the target first. Confirm the new WordPress Address and Site Address, the document-root or subdirectory location, and the HTTPS endpoint before sending traffic there.
- Back up both configurations. Keep the old
.htaccess, the edited version, and any host or proxy settings together so the change can be reversed. - Install specific rules first. Put page-level and directory-level redirects above broad host or scheme canonicalization rules. Preserve the generated WordPress block below them.
- Regenerate permalinks. Save Settings > Permalinks after the new location is active, then inspect the resulting rules for the correct base path.
- Validate representative traffic. Check the bare domain, an old post, an old page, a deeper path, an administrative path, both slash forms where relevant, and the final destination.
This sequence reduces the chance that a broad migration rule hides a missing page-specific redirect or that WordPress later replaces a hand-written line.
Test before making a redirect permanent
- Save the backup and edit a single rule. Avoid changing the generated WordPress block and custom migration rules at the same time.
- Start with a temporary status. This prevents a browser from caching an incorrect permanent destination while you verify the expression and target.
- Request the exact old URL. Test both slash forms when the pattern allows both, and include the correct hostname and scheme.
- Inspect the result. Confirm that the response goes to the intended destination and that the destination returns the expected page rather than another redirect or a 404.
- Probe neighboring paths. Request a similarly named page, a deeper path, and a query-string variant when those cases matter to the migration. This reveals accidental pattern matches.
- Clear relevant caches. Check browser, page-cache, CDN, and proxy layers so you are not reading an earlier redirect response.
- Switch to
R=301only after verification. Keep the final rule specific and leave theLflag in place to stop later rules from changing the result.
Diagnose 404s, loops, and ignored rules
| Symptom | Likely cause | What to check |
|---|---|---|
| The file changes nothing | The server is Nginx, mod_rewrite is unavailable, or Apache is configured to ignore overrides. |
Confirm the server type, ask the host whether mod_rewrite is loaded, and verify the directory’s override policy. |
| Old URL returns a 404 | The rule is in the wrong directory, the pattern does not match the per-directory path, or the generated rules are being reached first. | Check the active document root, remove any leading slash from the rule pattern, and move the custom rule above the WordPress block. |
| Redirect repeats or never reaches the page | The destination matches the old rule, two canonicalization rules disagree, or a reverse proxy reports the wrong scheme. | Request the destination directly, compare hostname and trailing-slash assumptions, and verify HTTPS termination and proxy headers. |
| The site shows a server error after editing | A malformed directive, expression, or flag has made the configuration invalid. | Restore the backup immediately, then reintroduce one corrected rule and check the host’s Apache error log or support guidance. |
| Rules disappear after saving permalinks | They were placed between the WordPress markers. | Move hand-written redirects and security rules outside # BEGIN WordPress and # END WordPress, then save permalinks again. |
If the baseline rules are missing or damaged, resave Settings > Permalinks. When the file is not writable, copy the rules displayed on that screen into the correct file. A missing mod_rewrite module cannot be fixed by changing the rule syntax; the host must provide the capability.
Keep security and maintenance rules separate
WordPress’s hardening guidance uses mod_rewrite for rules that block or protect sensitive files. Place those directives outside the generated WordPress markers for the same reason as redirects: WordPress can overwrite the marked section. Review every security rule for Multisite compatibility before deploying it, because a rule suitable for one site can interfere with other sites or mapped paths in a network.
Document why each permanent redirect exists, its old and new URL, and the date it was added. Remove obsolete rules only after confirming that links, bookmarks, and migration traffic no longer depend on them. Keep a known-good copy of the complete file, not just the line you most recently changed.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Choose the right redirect layer
| Mechanism | Best fit | Operational consideration |
|---|---|---|
Apache .htaccess |
Specific redirects on an Apache WordPress site where per-directory configuration is enabled. | Runs before WordPress when ordered correctly, but depends on Apache, mod_rewrite, permissions, and valid syntax. WordPress may overwrite rules inside its marked block. |
| WordPress or plugin-level redirect | Sites where administrators need to manage redirects through WordPress rather than edit server files. | Requires WordPress to load before the redirect is evaluated and is subject to the chosen tool’s configuration and maintenance. |
| Nginx server-block rules | Sites served by Nginx or a hosting stack that does not process .htaccess. |
Must be configured in the server or hosting platform; editing .htaccess will not change Nginx behavior. |
For a single old WordPress URL on Apache, a narrowly anchored rule outside the generated block is usually the most direct solution. For a host-wide scheme or domain migration, coordinate the server rule, WordPress URL settings, proxy behavior, and permalink regeneration as one change.
A reliable final configuration
The finished file should have a tested custom rule above the WordPress markers, an intact generated block below them, and a backup stored outside the live document root. The old path should match only the URLs intended for migration, the destination should not match the old rule, and the permanent status should be used only after testing confirms that the request reaches the correct final page.
Best Value
Frequently Asked Questions
Can I use .htaccess redirects on an Nginx WordPress site?
No. Nginx does not process Apache’s .htaccess files. Configure the equivalent redirect in the Nginx server block or through the hosting platform, and ask the host where those settings are managed.
Where should a RewriteRule go in .htaccess?
Put a specific custom rule above the # BEGIN WordPress marker. The generated front-controller rule is broad and should run only after page-specific redirects have had a chance to match.
Quick wins for a faster PC:
Scan for outdated or missing drivers - takes under a minuteDriver Scan →Repair Windows errors before they cause bigger problemsFix Now →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Why does my redirect return a 404?
Check that the file is in the active document root or WordPress directory, that the per-directory pattern has no leading slash, that mod_rewrite is loaded, and that the custom rule appears before the generated WordPress block. Resave Settings > Permalinks to restore baseline rules if needed.
Why is my redirect looping?
The destination may match the old-path rule, another canonicalization rule may disagree about hostname or trailing slash, or a reverse proxy may report HTTPS as HTTP. Request the destination directly and verify the proxy’s scheme handling.
Will saving WordPress permalinks delete my redirects?
It can delete rules placed between # BEGIN WordPress and # END WordPress. Keep hand-written redirects and security directives outside those markers.
Should I use 301 immediately?
Use a temporary redirect while testing so an incorrect destination is easier to change. Switch to R=301 only after the old URL, destination, nearby paths, and relevant cache layers have been checked.
Do I need to change WordPress settings during a domain or directory move?
Yes. Coordinate the WordPress Address, Site Address, hostname, HTTPS termination, installation directory, trailing-slash convention, and regenerated permalink rules. A redirect line alone does not complete a migration.
What if WordPress cannot write .htaccess?
Save the desired structure under Settings > Permalinks, copy the rules displayed on that screen, and paste them manually into the correct file. If the host does not permit overrides or lacks mod_rewrite, contact the host rather than changing syntax repeatedly.
The Bottom Line
On Apache, back up .htaccess, verify mod_rewrite and override support, keep WordPress’s generated block intact, and place an anchored custom rule above it. Test with a temporary redirect, inspect the destination and neighboring paths, clear caches, and use R=301 only when the old URL reaches the correct final page without a loop. On Nginx, configure redirects in the server-block layer instead.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




