Resources / Server & Hosting

How to redirect index.php to another domain

Updated September 20267 min readBy Thomas McKee · Building websites since 1996
Quick answer

The cleanest way to send an entire site to a new domain is a 301 redirect in .htaccess — it preserves every path, passes SEO value, and survives WordPress core updates (editing index.php itself does not). Replace the two domains and you're done:

.htaccess
# 301 an entire domain to a new one, keeping paths and query strings
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$ [NC]
RewriteRule ^(.*)$ https://newdomain.com/$1 [R=301,L]

Put it at the top of the old site's .htaccess, above any WordPress rules. Works identically on Apache and LiteSpeed. If you specifically need the PHP version — the thing this query is usually about — it's below, along with when each method is the right call.

Method 1: .htaccess (recommended)

The block above catches every request to the old domain — with or without www — and forwards it to the same path on the new domain. %{HTTP_HOST} matching prevents redirect loops if both domains point at the same server, $1 carries the path across, and query strings (?utm_source=...) pass through automatically with RewriteRule.

If you only need one page redirected rather than the whole site, mod_alias is simpler:

.htaccess
# Single page to a specific new URL
Redirect 301 /old-page/ https://newdomain.com/new-page/
NoteAvoid the bare Redirect 301 / https://newdomain.com/ one-liner for whole sites on WordPress — mod_alias and WordPress's own rewrite rules process independently, which occasionally produces doubled paths. The RewriteRule version doesn't have that problem.

Method 2: PHP redirect in index.php

Sometimes you can't touch .htaccess — a bare account with only an index.php, or a host that ignores override files. This goes at the very top of the file, before any output, including whitespace:

index.php
<?php
// 301 everything to the new domain, preserving the requested path
$target = 'https://newdomain.com' . ($_SERVER['REQUEST_URI'] ?? '/');
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $target);
exit;
WordPress warningDon't paste this into WordPress core's index.php — the next core update overwrites the file and your redirect silently vanishes. On a WordPress site, use the .htaccess method. The PHP method is for placeholder pages, retired custom sites, and hosts without rewrite support.

Which one, when

  • Moving a whole site to a new domain: .htaccess RewriteRule. Set it, then use Google Search Console's Change of Address tool on the old property.
  • Old domain is just a parked shell with one file: the PHP version is fine — it's the only thing that file will ever do.
  • Only some URLs move: individual Redirect 301 lines, or the RewriteRule pattern narrowed to those paths.
  • Temporary move (rare — seasonal campaigns, maintenance): use 302/307 instead of 301 so search engines keep the original URL indexed.

Test it before you trust it

Don't test 301s in your normal browser — browsers cache them aggressively, so a mistake can haunt you after you've fixed it. Use curl and check both the status and the Location header:

terminal
curl -sI https://olddomain.com/some/deep/page/?ref=test | grep -iE '^(HTTP|location)'

# You want exactly:
# HTTP/2 301
# location: https://newdomain.com/some/deep/page/?ref=test

Three things to verify: the status is 301 (not 302), the path and query string survived, and it happens in one hop. If you see http → https → www → new domain chains, collapse them — every extra hop wastes crawl budget and slows real visitors.

The mistakes that cost rankings

  1. Redirecting everything to the new homepage. Path-blind redirects turn every earned backlink into a soft-404 experience. Always preserve paths unless a page genuinely has no equivalent.
  2. Removing the redirect too soon. Google's redirect documentation treats permanent redirects as the signal that consolidates the old URL into the new one — keep it a minimum of 12 months, and if the old domain renewal is cheap, keep it forever.
  3. Forgetting Search Console. The Change of Address tool actively tells Google about the move instead of waiting for it to be inferred.
  4. Redirect loops. If both domains resolve to the same document root, a rule without the HTTP_HOST condition redirects the new domain to itself. The condition in Method 1 is not optional.
  5. Testing in a cached browser. See above — curl or a private window, always.
Key takeaways
  • Whole-site moves belong in .htaccess with a RewriteCond on HTTP_HOST — it survives WordPress updates and prevents loops.
  • The PHP header() method is for bare placeholder files only; core updates silently erase it on WordPress.
  • Always preserve paths and query strings — redirecting everything to the new homepage burns your backlinks.
  • Verify with curl -sI, never a normal browser tab: browsers cache 301s hard.
  • One hop, 301 status, 12+ months minimum — then tell Search Console via Change of Address.

Rather not touch the server yourself?

Domain moves, redirect maps, and rankings-safe migrations are standard work for our WordPress maintenance clients. One conversation, zero downtime.

See maintenance plans Call (417) 812-6313

FAQ

Does a 301 redirect pass SEO value to the new domain?

Yes. Google has confirmed that 301s pass full PageRank. What you can lose is anything that doesn't map — redirect page-to-page, not everything-to-homepage, and the transfer is about as clean as migrations get.

How long until rankings recover after a domain change?

With clean one-hop redirects and a Change of Address submission, most sites see rankings consolidate on the new domain within a few weeks to a few months. Volatility in the first weeks is normal; chains and dropped paths are what turn it into a real loss.

301 or 302 — which should I use?

301 for anything permanent, which domain moves almost always are. 302 tells search engines to keep the old URL indexed, which is exactly what you don't want here.

Do I need to update my backlinks?

You don't need to — the 301 carries them — but for your most valuable links (directories, partners, profiles you control), updating to the new domain directly removes a dependency on the redirect staying up forever.

Does this work on LiteSpeed servers?

Yes. LiteSpeed reads Apache-style .htaccess rules natively, including everything on this page. That covers most cPanel hosting, ours included.

Thomas McKee Website Design & SEO Solutions logo
Thomas McKee

Owner, Thomas McKee Website Design & SEO Solutions — building, hosting, and ranking websites from Springfield, Missouri since 1996. 600+ projects, six straight years Best SEO Springfield MO. What we do