Pagina in fase di traduzione e/o adattamento
Pagina da rivedere e/o aggiornare
Creare una Pagina 404
Anche se lavorate duramente per fare in modo che ogni link presente nelle vostre pagine porti ad una specifica pagina, c'é sempre la possibilità che un link possa essere sbagliato e dare quindi vita ad un famoso ERRORE 404 PAGINA NON TROVATA.
Non tutto é perduto. Se i vostri visitatori imbattono in un errore, perché non essere un amministratore premuroso e presentare loro una pagian più utile che non un normale errore di "PAGINA NON TROVATA" ?
Questa lezione vi insegnerà come modificare i vostri messaggi di "errore" e "pagina non trovata" in modo che risultino più utili per i vostri visitatori. Mostreremo anche come assicurarci che il server visualizzi i vostri messaggi di aiuto. Finalmente poi vedremo come creare una pagina di errore personalizzata che rispecchi lo stile del vostro Tema.
Un Minimo di Prevenzione
Alcuni errori sono evitabili, specie se fate attenzione a fare attenti controlli ? su tutti i vostri link. Inoltre, se state cancellando un articolo popolare ma datato, considerate l'opportunità di cancellare il contenuto dell'articolo, sostituendolo con un link che rimandi i visitatori alla nuova pagina.
Comprendere il sistema degli Errori
I visitatori si imbattono spesso in errori anche nei migliori siti web. In qualità di amministratori del sito, voi potreste cancellare articoli datati, ma altri siti web potrebbero avere dei link che conducano proprio a quell'articolo.
Quando un utente clicca su un link che riporta ad una pagina inesistente, il server web manderà al visitatore un messaggio di errore noto come 404 Pagina Non Trovata.
A meno che il webmaster non abbia scritto un messaggio di errore personalizzato, il messaggio standard apparirà in semplice formato testuale, lasciando all'utente una sensazione di smarrimento.
Molti utenti sono capaci di premere il bottone per tornare indietro, ma potreste aver perso un visitatore al quale non piace perdere il proprio tempo nella ricerca di informazioni. Ed allora, per non perdere visitatori, vi sarà utile fornire un messaggio personalizzato che contenga almeno un link alla vostra home page.
Il modo migliore di gestire gli errori è quello di riconoscere l'errore ed aiutarli a trovare la loro strada. Questo implice la creazione di una pagina personalizzata dell'Errore o la modifica di una delle pagine che trovate con il vostro Tema per WordPress.
Modificare una Pagina di Errore 404
Il tema predefinito di WordPress ha un file 404.php, ma non tutti i Temi hanno il proprio template personalizzato per l'errore 404. Se lo hanno, esso sarà chiamato 404.php. WordPress userà automaticamente quella pagina se si verificherà un errore di Pagina Non Trovata.
Il normale file 404.php accompagnato al vostro Tema funzionerà, ma dirà quello che voi volete che dica, e offrirà il tipo di aiuto che volete che offra? Se la risposta è no, allora sarà vostro desiderio personalizzare il messaggio presente nel template.
Per modificare il file con il template per l'errore 404 del vostro Tema:
- Aprite il pannello di amministrazione del vostro WordPress:
- Scegliete il menù Aspetto.
- Scegliete la pagina Editor Temi.
- Controllate se il vostro tema imcluda un "Template 404" nella lista dei file.
- Cliccate sul linl 'Template 404' nella parte destra della pagina.
- Modificate il messaggio scrivendo quello che volete dire.
- Salvate le vostre modifiche. (Se il template non è scrivibile dal server, controllate i permessi dei file ?.)
Quando sarete nel punto di esaminare e modificare il file del vostro template 404, date una occhiata alla semplice struttura del file 404.php. Di base ha i tag che visualizzano la testata, la barra laterale e il piè di pagina, ed inoltre un'area per il vostro messaggio:
<?php get_header(); ?>
<div id="content" class="narrowcolumn">
<h2 class="center">Error 404 - Not Found</h2>
- </div>
<?php get_sidebar(); ?> <?php get_footer(); ?>
So, to change the error message your visitor sees, revise the text within the h2 heading and if necessary, add more paragraphs below that.
Creating an Error 404 Page
If your WordPress Theme does not include a template file named 404.php, you can create your own.
Because every theme is different, there is no guarantee that copying over the 404.php template file found in the WordPress Default Theme will work, but it's a good place to start. The error page you copy from the Default Theme will adopt the style of the current theme because it actually calls the header and footer of the current theme. That's less work for you, and you may only have to edit the message to suit your particular needs.
To use the 404.php template file from the WordPress Default Theme:
- Copy the file
/wp-content/themes/default/404.phpinto the directory of your current theme. - Then, as described in the previous section, edit the error message to present your desired error message.
If copying the default 404.php into your theme directory does not work well with your theme, you have some other choices:
- Change the Default Theme's
404.phptemplate file's header, sidebar, footer, and other codes to match the rest of the Theme's layout.
Or
- Copy the
index.phpfile of your current theme to a file called404.php. - Open that file and delete all sections dealing with posts or comments, see The Loop ?.
- Then, edit your 404 error message.
Tips for Error Pages
There are various improvements you can make to your 404 Error web pages so let's look at some of your options.
Sending Proper Headers
By default, WordPress continues to send 404 pages as if they were fine. To make search engines like Google spider these pages correctly, you can add this line to the top of your theme's 404.php file.
<?php header("HTTP/1.1 404 Not Found"); ?>
Writing Friendly Messages
When an error message is displayed, you can say many things to help a visitor feel reassured they've only encountered a minor glitch, and you're doing the best you can to help them find the information they want. You can say something clever like:
"Oops, I screwed up and you discovered my fatal flaw. Well, we're not all perfect, but we try. Can you try this again or maybe visit our <a title="Our Site" href="http://example.com/index.php">Home Page</a> to start fresh. We'll do better next time."
Or, say something shorter and sweeter. Almost anything you say is better than 404 Error Page Not Found. You can find more information about writing 404 Error pages on the Internet, like List Apart's Perfect 404.
As an implementation of the Perfect 404 page, this solution will tell the user it's not their fault, if it isn't and email the webmaster, according to WordPress's database. Helpful 404 page
When a visitor gets a 404 error page, it can be intimidating, and unhelpful. Using Wordpress, you can take the edge off a 404 and make it helpful to users, and yourself, too, by emailing whenever the user clicks a link to a non-existent page. If you use this, don't forget o
<p>You
<?php
#some variables for the script to use
#if you have some reason to change these, do. but wordpress can handle it
$adminemail = get_bloginfo('admin_email'); #the administrator email address, according to wordpress
$website = get_bloginfo('url'); #gets your blog's url from wordpress
$websitename = get_bloginfo('name'); #sets the blog's name, according to wordpress
if (!isset($_SERVER['HTTP_REFERER'])) {
#politely blames the user for all the problems they caused
echo "tried going to "; #starts assembling an output paragraph
- $casemessage = "All is not lost!";
} elseif (isset($_SERVER['HTTP_REFERER'])) {
#this will help the user find what they want, and email me of a bad link
echo "clicked a link to"; #now the message says You clicked a link to...
#setup a message to be sent to me
$failuremess = "A user tried to go to $website"
.$_SERVER['REQUEST_URI']." and received a 404 (page not found) error. ";
$failuremess .= "It wasn't their fault, so try fixing it.
They came from ".$_SERVER['HTTP_REFERER'];
mail($adminemail, "Bad Link To ".$_SERVER['REQUEST_URI'],
$failuremess, "From: $websitename <noreply@$website>"); #email you about problem
$casemessage = "An administrator has been emailed
about this problem, too.";#set a friendly message
}
echo " ".$website.$_SERVER['REQUEST_URI']; ?>
and it doesn't exist. <?php echo $casemessage; ?> You can click back
and try again or search for what you're looking for:
<?php include(TEMPLATEPATH . "/searchform.php"); ?>
</p>
Add Useful Links
If you encounter a "page not found" situation on the WordPress site, it is filled with helpful links to direct you to the various categories and areas of information within the WordPress site. Check it out at http://wordpress.org/brokenlink.php.
To add similar useful links to your 404 page, create a list, or a paragraph, so the visitor can easily determine which section might be useful to visit. Information of that nature is much better than having the user just reach a dead-end. To help you understand how to link to documents within your site, especially to Pages ? and Categories, see Linking_Posts_Pages_and_Categories ?.
Testing 404 Error Messages
To test your custom 404 page and message, just type an URL address into your browser for your website that doesn't exist. Make one up or use something like:
http://example.com/fred.php
This is sure to result in an error unless you actually have a php file called fred. If your error page doesn't look "right", you can go back and edit it so it works correctly and matches your Theme's look and feel.
==Help Your Server Find the 404 Page==
By default, if WordPress cannot find a particular page it will look for the 404.php web page. However, there may be cases where the web server encounters a problem before WordPress is aware of it. In that case, you can still guarantee that your web server sends the visitor to your 404.php template file by configuring your web server for custom 404 error handling.
To tell your web server to use your custom error files, you'll need to edit the .htaccess file in the main directory (where main index.php file resides) of your WordPress installation. If you don't have an .htaccess file, see on how to create an .htaccess file.
To ensure the server finds your 404 page, add the following line to your .htaccess file:
ErrorDocument 404 /index.php?error=404
The url /index.php is root-relative, which means that the forward slash begins with the root folder of your site. If WordPress is in a subfolder or subdirectory of your site's root folder named 'wordpress', the line you add to your .htaccess file might be:
ErrorDocument 404 /wordpress/index.php?error=404
Questions About Error Files
- Why not just hard code the path all the way to the 404.php file?
- By allowing index.php to call the error file, you ensure that the 404.php file used will change automatically as you change your theme.
- What happens if I switch to a theme that does not have a 404.php file?
- Visitors clicking on a broken link will just see a copy of the home page of your WordPress site (
index.php), but the URL they see will be the URL of the broken link. That can confuse them, especially since there is no acknowledgement of the error. But this is still better than a getting a "NOT FOUND" message without any links or information that could help them find what they seek.
Other Error Numbers
Here are some other standard web error numbers.
- 401 failed authorization
- 403 forbidden
- 404 missing file
- 500 internal server error
Authorization errors are handled by the WordPress program.
Category:Design and Layout ? Category:WordPress Lessons ? Category:Advanced Topics ? Category:Templates ?