08Website know-how & errors
Fix error 404 and set up redirects
404 Not Found on your website? Find the cause, set up 301 redirects, resolve redirect loops and repair WordPress permalinks at Nexthosting.
In short
A 404 means: there is nothing at this address. First check the spelling, upper and lower case, and the location of the file. With WordPress, a click on Einstellungen (Settings), Permalinks, Änderungen speichern (Save Changes) often helps. If a page moves, you point the old address to the new one with a 301 redirect.
Why 404?
Section titled “Why 404?”The server understood the request but finds no matching file or page. To begin with, that is not a server error.
The simple causes first
Section titled “The simple causes first”- Typo in the address or in the link.
- Upper and lower case. On Linux servers,
Bild.jpgandbild.jpgare two different files. - File is in the wrong folder, for example because an extra subfolder was created during the upload.
- File was deleted or renamed.
- Extensions:
seite.htmlis notseite.htm. - Wrong domain assignment: The domain points to a different folder.
- Spaces and umlauts in file names cause trouble. Use
a-z,0-9and hyphens.
WordPress: subpages return 404
Section titled “WordPress: subpages return 404”If the home page works but posts and pages show 404, the rewrite rules (permalink rules) in the .htaccess are missing or outdated.
- Log in to the WordPress admin area.
- Open Einstellungen (Settings), then Permalinks.
- Without changing anything, click Änderungen speichern (Save Changes). WordPress rewrites the rules.
- Still not loading? Check whether the
.htaccessfile exists in the main directory and is readable by the web server (File permissions).
The default WordPress rules look like this:
# BEGIN WordPress<IfModule mod_rewrite.c>RewriteEngine OnRewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]RewriteBase /RewriteRule ^index\.php$ - [L]RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule . /index.php [L]</IfModule># END WordPressMore help in Solving WordPress problems.
A custom 404 page
Section titled “A custom 404 page”A good 404 page says that the page is missing and offers ways onward: search, home page, key sections. With WordPress the theme provides it (404.php). With a static site you create 404.html and assign it in the .htaccess:
ErrorDocument 404 /404.htmlImportant: the page must really deliver the status code 404, not 200. Otherwise search engines take it for a normal page (“soft 404”).
Set up a 301 redirect
Section titled “Set up a 301 redirect”A 301 redirect sends visitors and search engines from an old address to a new one and passes on the search value of the old page. Use it when you move pages, change the structure or switch domains.
A single page
Section titled “A single page”Redirect 301 /alte-seite/ https://meinprojekt.de/neue-seite/With a pattern (mod_rewrite)
Section titled “With a pattern (mod_rewrite)”RewriteEngine OnRewriteRule ^blog/(.*)$ https://meinprojekt.de/ratgeber/$1 [R=301,L]Move a whole domain
Section titled “Move a whole domain”RewriteEngine OnRewriteCond %{HTTP_HOST} ^(www\.)?alte-domain\.de$ [NC]RewriteRule ^(.*)$ https://neue-domain.de/$1 [R=301,L]- The rules belong in the
.htaccessin the main directory. It works at Nexthosting (Apache); see .htaccess basics. - Place redirects before the WordPress rules (that is, above
# BEGIN WordPress). - Redirect to the matching new page, not everything to the home page. Otherwise search engines treat it as a soft 404.
- Test with
curl:
curl -sI https://meinprojekt.de/alte-seite/ | grep -E "^HTTP|^location"With WordPress, redirects also work with a plugin. For a few redirects the .htaccess is quicker.
| Goal | Code | When |
|---|---|---|
| Page has moved permanently | 301 (or 308) | Rebuild, new domain |
| Temporarily elsewhere (maintenance, campaign) | 302 (or 307) | Short term |
| Page removed on purpose | 410 | “Gone for good” |
Redirect loops
Section titled “Redirect loops”The browser reports “ERR_TOO_MANY_REDIRECTS” or “The page redirected you too many times”. Two rules send visitors round in circles.
Common causes:
httpis redirected tohttps, and a plugin redirects back tohttp.- In WordPress, “WordPress Address” and “Site Address” differ (www / without www, http / https).
- A proxy or Cloudflare in “Flexible” mode talks to the server over http, and the server redirects to https again.
- Two rules in the
.htaccess(www and without www) work against each other.
- Delete the site's cookiesRules out login loops
- Rename .htaccessDoes the site work now?
- Add rules back one by oneTest after each rule
- Check the WordPress addressesSettings, General
- Check the Cloudflare modeFull or Full (strict)
Follow the path with curl:
curl -sIL --max-redirs 10 https://meinprojekt.de/ | grep -E "^HTTP|^location"404 and search engines
Section titled “404 and search engines”Individual 404 pages do no harm. What matters is that important pages do not disappear. After a relaunch, check the list of your previous addresses and redirect them.
Frequently asked questions
Section titled “Frequently asked questions”Why does my page show 404 although the file is there?
Section titled “Why does my page show 404 although the file is there?”Usually the upper/lower case or the folder is wrong, or the domain points to a different folder.
How long does a 301 redirect last?
Section titled “How long does a 301 redirect last?”Browsers remember it for a long time. So test new redirects first with 302 or with curl.
Do I have to redirect www to without www?
Section titled “Do I have to redirect www to without www?”You should choose one variant as the main address and redirect the other. Example in .htaccess basics.