Redirect with a meta refresh tag
Open the old page and add one line to its head. Keep the rest of the head if you like, the browser only needs the refresh line.
<head>
<meta charset="utf-8">
<meta http-equiv="refresh" content="0; url=https://www.cafe.com/new-menu">
<title>Our menu has moved</title>
</head> http-equiv="refresh" tells the browser to load another address. Everything it needs to know is
packed into content: first the number of seconds to wait, then a semicolon, then
url= and the new address. Here are the common versions:
<!-- Go right away -->
<meta http-equiv="refresh" content="0; url=https://www.cafe.com/new-menu">
<!-- Wait 5 seconds, then go -->
<meta http-equiv="refresh" content="5; url=https://www.cafe.com/new-menu">
<!-- A page in the same folder -->
<meta http-equiv="refresh" content="0; url=new-menu.html"> - The number is seconds, not milliseconds.
5waits five seconds. Use whole numbers; anything after a dot is ignored, so1.9waits one second. - A relative URL works. It’s worked out from the old page’s own address, just like a link. On
https://www.cafe.com/menu/old.html,url=new.htmlgoes to/menu/new.html, andurl=/new-menugoes tohttps://www.cafe.com/new-menu. Relative URLs has the full table. - The quotes go around the whole content value. If you want quotes around the address too, use
single ones inside:
content="0; url='new-menu.html'". Browsers also accept it withouturl=(content="0; new-menu.html"), but the long form is easier to read. - It belongs in the head. Browsers will still follow it from the body, but a validator flags it there, and a tag near the top of the head is found sooner.
-
If the new address has spaces or odd characters in it, encode them first (
%20for a space). The URL Encoder does it for you.
One mistake shows up a lot: closing the quote after the number. The browser then reads
content="0" with no address at all, which means “reload this page now”, and it does that forever.
In Chrome it reloads dozens of times a second.
<meta http-equiv="refresh" content="0"; url="new-menu.html"> <meta http-equiv="refresh" content="0; url=new-menu.html"> Which redirect to use
There are three ways to send someone on, and they happen at different times. A server redirect answers before any page is sent. A meta refresh needs the old page to arrive first. A JavaScript redirect needs the old page and its script to run. Earlier is better, for people and for search engines.
| What you can change | Use | Why |
|---|---|---|
Server settings, .htaccess or your host’s redirect page | A 301 redirect | Fastest, works for every file type, and search engines move the old page’s place in results to the new one. |
| Only the HTML files | Meta refresh with 0 | Works with no server access. Google reads an instant one as a permanent move. |
| The page has to decide in the browser | location.replace() | For choices the server can’t know, like the shop someone picked in a menu. |
| Both pages should stay up | A canonical link, no redirect | People can still open both. Search engines are told which one to list. |
A delayed redirect with a message and a link
Sometimes you want people to see that the page moved, so they can update a bookmark. Give the refresh a few seconds, say so on the page, and always add a plain link. The link is for anyone whose browser has automatic redirects turned off, and for anyone who doesn’t want to wait.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="refresh" content="5; url=new-menu.html">
<title>Our menu has moved</title>
</head>
<body>
<h1>Our menu has moved</h1>
<p>You'll be taken to the new menu in 5 seconds.</p>
<p>Not moving? <a href="new-menu.html">Open the new menu</a>.</p>
</body>
</html> Our menu has moved
You’ll be taken to the new menu in 5 seconds.
Not moving? Open the new menu.
Put the same address in the link and in the refresh, and if you change one later, change the other. Click the link once after you upload the page: it’s the quickest test that the address is right.
Make the delay long enough to read the message, or don’t use one. Two seconds is too quick to read, and a timed
redirect can be hard on some people, see accessibility below. To Google, a delayed meta
refresh looks like a temporary move, so for a page that’s gone for good, 0 or a server 301 is the
better signal.
Redirect with JavaScript
JavaScript can change the address too. You’ll see two lines everywhere, and they treat the Back button differently.
<script>
location.replace('https://www.cafe.com/new-menu');
</script> <script>
location.href = 'https://www.cafe.com/new-menu';
</script> location.replace() swaps the old page out of the history, as if it was never there. Press Back on the
new page and you go to wherever you were before. Setting location.href adds a new step instead, like
a click. Do that from a timer and Back takes people to the old page, which sends them forward again, so they can’t
get back at all. (Browsers quietly treat a redirect that runs while the page is still loading as a replace, which is
why you won’t always see this, but replace() is the safe habit for redirects.)
Keep location.href for moves the visitor asked for, like picking a shop and pressing Go. Then Back
brings them back to the list. The Go button matters: changing the page the moment the select changes would move
keyboard users on as soon as they press the down arrow.
<form id="pick">
<label for="shop">Pick your shop</label>
<select id="shop">
<option value="">Choose one</option>
<option value="/menu/downtown">Downtown</option>
<option value="/menu/harbor">Harbor</option>
</select>
<button>Go</button>
</form>
<script>
document.getElementById('pick').addEventListener('submit', (event) => {
event.preventDefault();
const shop = document.getElementById('shop');
if (shop.value) location.href = shop.value;
});
</script> For a plain “this page moved”, JavaScript is the weakest of the three. It does nothing when scripts are off, and search engines have to run the page before they see it. Google’s own advice is to use a JavaScript redirect only when a server or meta refresh redirect isn’t possible. If you’re not sure where the script tag goes, how to link JavaScript to HTML covers it.
A 301 redirect on the server
A server redirect sends back a short answer instead of a page: the status code 301 and a
Location header with the new address. The browser goes there straight away, and the old page never
loads. Where you set it up depends on the server.
Apache: one line in .htaccess
# .htaccess in your site's top folder
Redirect 301 /old-menu.html /new-menu
The old path comes first and starts with /. The new one can be a path on the same site or a full
address like https://www.cafe.com/new-menu. It also matches everything under the old path: with
Redirect 301 /menu /food, a visit to /menu/drinks lands on /food/drinks.
.htaccess only works on Apache servers that allow it, and the file name starts with a dot, so your
computer may hide it.
Nginx: return 301
server {
server_name www.cafe.com;
location = /old-menu.html {
return 301 /new-menu;
}
} location = matches that one exact path. Check the file with sudo nginx -t, then load it
with sudo nginx -s reload.
PHP: a header before anything else
<?php
header('Location: https://www.cafe.com/new-menu', true, 301);
exit;
The header() call has to come before any HTML, even a blank line, or PHP may refuse with
“headers already sent”, and the redirect doesn’t happen. Without the 301, PHP sends a 302. exit stops the rest of the file
from running.
No server access? Look for a redirect setting
Most hosting dashboards and site builders have a Redirects page where you type the old path and the new address, and they send a proper 301 for you. WordPress has redirect plugins that do the same. Search your host’s help pages for “redirect” before you reach for a meta tag.
301, 302, 307 and 308: which status code?
The number tells browsers and search engines whether the move is for good. Most of the time you want a 301.
| Code | Name | Meaning | Use it for |
|---|---|---|---|
| 301 | Moved Permanently | The page lives at the new address now. | A renamed page, a new domain, http to https |
| 302 | Found | Go there for now, the old address stays the real one. | A short sale page, a page under repair |
| 307 | Temporary Redirect | Like 302, but a form sent with POST is sent on as POST. | Temporary moves of forms and APIs |
| 308 | Permanent Redirect | Like 301, but keeps the POST. | Permanent moves of forms and APIs |
- With 301 and 302, browsers may turn a POST into a GET at the new address, so the form data is dropped. For a normal page nobody sends a form to, that doesn’t matter.
- Browsers can remember a 301 for a long time and skip the old address without asking the server. If you aren’t sure about the new address yet, test with a 302 first and switch to 301 when it’s right.
- Search engines move the old page’s place in their results to the new address after a 301 or 308. After a 302 or 307 they usually keep listing the old one.
Redirect a whole site to https or www
This one belongs on the server, not in your HTML. A meta tag would have to go on every page, and it can only run
after the insecure http page has already been sent. Pick one address for your site (with or without
www, always https) and 301 everything else to it, keeping the path, so
http://cafe.com/menu lands on https://www.cafe.com/menu and not on the home page. Many
hosts have a single “force HTTPS” switch for the first part. On your own Nginx server it looks like this:
server {
listen 80;
server_name cafe.com www.cafe.com;
return 301 https://www.cafe.com$request_uri;
} $request_uri is the path plus anything after the ?, so every old link keeps working. The
https address without www needs the same kind of redirect in your https server block.
A canonical link or a redirect?
A redirect moves people and search engines. A canonical link moves only the search engines: people can still open both pages, and the copy says “list this other address instead of me”. It goes in the head of the copy:
<link rel="canonical" href="https://www.cafe.com/menu">
Use it when the same menu is reachable at two addresses on purpose, say /menu and
/menu?sort=price. If the old page has no reason to exist any more, redirect it. More in
what a canonical URL is, and the
Meta Tag Generator writes the tag along with the rest of your head.
Accessibility and SEO: things to watch
- A page that changes on its own can lose people. Someone using a screen reader, or a slow reader,
may be in the middle of the message when the page swaps under them. The accessibility guidelines (WCAG) list a
timed meta refresh redirect as a failure of “Timing Adjustable” (failure F40), while an instant one with
0is fine (technique H76). So: no delay, or no automatic redirect at all, only the link. - Don’t reload a page on a timer, such as
content="60"(no URL) on a news page, unless people can turn it off. It loses their place on the page, and WCAG lists it as failure F41. - Point straight at the final page. Old to new to newest is a chain, and every hop is another wait. When you move a page a second time, update the first redirect too.
- Send each old page to its closest match. If every old address goes to the home page, Google may treat those redirects as missing pages (a soft 404), and people don’t find what they clicked for.
- Keep redirects for a long time. Old links in emails, bookmarks and other sites keep coming for years. Google suggests at least a year for a site move. An old redirect rarely gets in the way.
How to check a redirect
The address bar only shows where you ended up. To see the redirect itself, use the browser’s developer tools:
- Open a new tab and press F12 (Cmd+Option+I on a Mac), then pick the Network tab.
- Tick Preserve log, or the list clears when the new page loads.
- Type the old address and press Enter. The first row shows the status, like
301. Click it and look under Headers forLocation, the address it sent you to.
A meta refresh looks different there: the old page comes back as 200, a normal page, and the new page
follows it as a separate row. From a terminal, curl -I shows only the headers:
curl -I https://www.cafe.com/old-menu.html HTTP/2 301
location: https://www.cafe.com/new-menu
Add -L (curl -IL) to follow every hop, which shows a chain at a glance. A meta refresh
isn’t a header, so curl shows 200 for it. To find the tag, fetch the page and search it:
curl -s https://www.cafe.com/old-menu.html | grep -i refresh You can also read the old page’s source with Ctrl+U before it moves on (how to view the source of a page). With a delay, there’s time.
When a redirect doesn’t work
- The page keeps reloading. Look at the quotes in your meta tag.
content="0"; url="…"has no address in it, so it reloads itself. The whole value goes in one pair of quotes:content="0; url=new-menu.html". - “Redirected you too many times”. Chrome shows
ERR_TOO_MANY_REDIRECTSwhen two addresses point at each other, for example an https rule on the server and an http rule in a plugin. Follow the hops withcurl -ILand remove one of them. - You fixed it, but the browser still goes to the old place. It remembered the 301. Try a private window, or clear the browser’s cache for that site.
- .htaccess does nothing. The server may not be Apache, or it may not read
.htaccessfiles. Check that the file is in the right folder and really starts with a dot, and that it isn’t saved ashtaccess.txt. - A 404 after the redirect. The new address is wrong. Relative addresses are worked out from the old
page’s folder, so
url=new-menu.htmlon/menu/old.htmllooks for/menu/new-menu.html. Start the path with/or use the full address. - The meta refresh doesn’t run inside a frame. A page shown in a sandboxed iframe without
allow-scriptscan’t redirect itself, and Chrome logs “Refused to execute the redirect” in the console. That’s also why this page shows redirects as code to copy, not as live boxes.
Questions people ask
How do I redirect to another page in HTML?
Add a meta refresh tag to the head of the old page: <meta http-equiv="refresh" content="0; url=https://www.cafe.com/new-menu">. The 0 is the number of seconds to wait, and the address after url= is where the browser goes. A relative address like url=new-menu.html works too. If you can change your server’s settings, a 301 redirect there is better for a page that has moved for good.
What is a 301 redirect?
It’s an answer from the server that says “this page has moved permanently” and gives the new address in a Location header. The browser goes straight to the new address, and search engines move the old page’s place in their results to it. You set it up in the server, for example with Redirect 301 /old-menu.html /new-menu in an Apache .htaccess file, or in your host’s redirect settings.
What is the difference between a 301 and a 302 redirect?
A 301 means the page moved for good, and a 302 means it moved for now and the old address is still the real one. Search engines usually switch to the new address after a 301 and keep the old one after a 302. Browsers can remember a 301 for a long time, so if you aren’t sure about the new address yet, test with a 302 first. 307 and 308 are the same pair, except that a form sent with POST stays a POST.
Is a meta refresh redirect bad for SEO?
Not if it has no delay. Google treats an instant meta refresh (content="0; url=...") as a permanent redirect, and a delayed one as temporary. A server 301 is still the better choice when you can set one up, because it’s faster and works before the old page loads. Search engines have to run a JavaScript redirect before they see it, so that one comes last.
How do I redirect after 5 seconds in HTML?
Put 5 in front of the address: <meta http-equiv="refresh" content="5; url=new-menu.html">. Say on the page that it will move, and add a normal link to the new page for anyone who doesn’t want to wait or has automatic redirects turned off. Keep in mind that a timed redirect can catch people halfway through reading, and the accessibility guidelines (WCAG) count it as a failure, so no delay is usually kinder.
Why does my page keep reloading instead of redirecting?
The quotes in the meta tag are probably in the wrong place. In content="0"; url="new.html" the content value is only 0, and a refresh with no address reloads the same page, over and over. The whole value goes inside one pair of quotes: content="0; url=new.html".
What is the difference between location.replace() and location.href?
Both send the browser to a new address with JavaScript. location.replace() swaps the current page out of the history, so the Back button skips it. Setting location.href adds a new history entry, like clicking a link, so Back returns to the old page, which can send people forward again if it redirects on a timer. Use replace() for redirects and location.href for moves the visitor asked for.
Should I use a redirect or a canonical tag?
Use a redirect when the old page shouldn’t exist any more: it moves people and search engines. Use a canonical link, <link rel="canonical" href="https://www.cafe.com/menu"> in the head of the copy, when both addresses should keep working for people, like /menu and /menu?sort=price, and you only want search engines to list one of them.
How do I check if a redirect is working?
Open the browser’s developer tools (F12), pick the Network tab, tick Preserve log and load the old address. The first row shows the status, like 301, and the Location header shows where it sent you. In a terminal, curl -I followed by the old address shows the same. A meta refresh shows up as a normal 200 page followed by the new one, because it’s part of the HTML, not a header.
Keep going
Tools that help
- URL Encoder & Decoder Turns spaces and symbols in a new address into %20-style codes.
- Meta Tag Generator Writes the title, description, canonical and social tags for a page’s head.
- HTML Link Generator Builds the “Open the new page” link for a moved-page notice.
- HTML Validator Spots a meta tag with broken quotes before you upload it.
More tutorials
- How to Center Text, a Div or an Image in HTML & CSS
- How to Add a Line Break in HTML
- How to Add Spaces in HTML
- How to Change Text Color in HTML
- How to Change Background Color in HTML
- How to Add an Image in HTML
- What Is a URL? Meaning, Parts and Examples
- How to Get an Image URL
- Canonical URL
- What Is HTML? What It Stands For and How It Works
- How to Create, Open and Run an HTML File
- How to Link CSS to HTML
- How to See the HTML Code of a Website
- How to Link JavaScript to HTML with the script Tag
- HTML hr Tag
- HTML Comments
- How to Make Text Bold in HTML
- HTML Font and Font Size
- All tutorials