Photo Resize U N I T CO N V E R T E R Internet of Things Arduino About Us
ESP8266 handle not found pages and
redirect
Our e-Books
February 2, 2018 ESP8266 ESP8266, NodeMCU, NotFound, Redirect
NodeMCU Communication Methods and
Protocols: Upgrade Your Knowledge
In this tutorial we learn NodeMCU Web Server request handling and redirect to root
location. For more details on creating HTML web Server with ESP8266 Read here.
All web related requests are handled through HTTP protocol.
Zero to Hero: ESP8266: Become Super hero
of Internet of Things
Example code of Page not found handling and
redirect to root location
Step 1: Create ESP8266 Server
This line define server object. uses ESP8266WebServer.h file
ESP8266WebServer server(80);
Measurement Made Simple with Arduino:
Step 2: Setup web pages to serve on client Ultimate e-Book for all your measurement
needs at one place.
In this code we setup page to server on root i.e. “/”. we call handleRoot subroutine when
client request web page at root location.
Another is NotFound location handling. i.e. handleNotFound
//Initialize
Webserver server.on("/",handleRoot);
server.onNotFound(handleNotFound);
server.begin();
Step 3: Con igure not found page subroutine Arduino Beginners Guide: Get started with
arduino in simple steps
In this first we send header location as root to client and send a request to redirect to that
i.e. HTTP 302
void handleNotFound()
{
server.sendHeader("Location", "/",true); //Redirect to our html web
page
server.send(302, "text/plane","");
}
Complete Code to demonstrate Web Page Redirect
in ESP8266
Join Circuits4You community
This is simple code with broken link. When broken link is clicked it redirects to the root
location. You can create custom 404 page and redirect to it.
/*
* ESP8266 Redirect Web Request
*
*/
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
//ESP AP Mode configuration
const char *ssid = "Redirect-demo-circuits4you.com";
ESP8266WebServer server(80);
Ad
void handleNotFound(){
server.sendHeader("Location", "/",true); //Redirect to our html web
page
server.send(302, "text/plane","");
} Recent Posts
Best Online JSON Editor Tool to Edit JSON
void handleRoot() {
Online
server.send(200, "text/html", "<html><body>Hello from ESP <br><a
href='/Thispage'>Failed Link</a></body></html>"); ESP8266 IoT Based RGB LED Strip Controller
}
ESP8266 BMP180 pressure sensor interface
void setup() { ESP8266 weather station using Arduino IDE
delay(1000); ESP8266 URL Encode Decode Example
Serial.begin(115200);
Serial.println();
//Initialize AP Mode
WiFi.softAP(ssid); //Password not used Ad
IPAddress myIP = WiFi.softAPIP();
Serial.print("Web Server IP:");
Serial.println(myIP);
//Initialize Webserver
server.on("/",handleRoot);
server.onNotFound(handleNotFound);
server.begin();
}
void loop() {
server.handleClient();
}
Upload code and see how it handles not found link on the page.
More on HTTP protocol Messages
When a browser requests a service from a web server, an error might occur.
This is a list of some commonly used HTTP status messages that might be returned:
2xx: Successful
MESSAGE: DESCRIPTION:
The request is OK (this is the standard response for successful HTTP
200 OK
requests)
201 Created The request has been fulfilled, and a new resource is created
The request has been accepted for processing, but the processing has not
202 Accepted
been completed
203 Non-Authoritative The request has been successfully processed, but is returning information
Information that may be from another source
The request has been successfully processed, but is not returning any
204 No Content
content
The request has been successfully processed, but is not returning any
205 Reset Content
content, and requires that the requester reset the document view
The server is delivering only part of the resource due to a range header sent
206 Partial Content
by the client
3xx: Redirection
MESSAGE: DESCRIPTION:
A link list. The user can select a link and go to that location. Maximum five
300 Multiple Choices
addresses
301 Moved
The requested page has moved to a new URL
Permanently
302 Found The requested page has moved temporarily to a new URL
303 See Other The requested page can be found under a di erent URL
304 Not Modified Indicates the requested page has not been modified since last requested
306 Switch Proxy No longer used
307 Temporary
The requested page has moved temporarily to a new URL
Redirect
308 Resume Used in the resumable requests proposal to resume aborted PUT or POST
Incomplete requests
4xx: Client Error
MESSAGE: DESCRIPTION:
400 Bad Request The request cannot be fulfilled due to bad syntax
The request was a legal request, but the server is refusing to respond to it. For
401 Unauthorized
use when authentication is possible but has failed or not yet been provided
404 Not Found The requested page could not be found but may be available again in the future
408 Request
The server timed out waiting for the request
Timeout
414 Request-URI The server will not accept the request, because the URL is too long. Occurs when
Too Long you convert a POST request to a GET request with a long query information
5xx: Server Error
MESSAGE: DESCRIPTION:
A generic error message, given when no more specific message is
500 Internal Server Error
suitable
The server was acting as a gateway or proxy and received an invalid
502 Bad Gateway
response from the upstream server
503 Service Unavailable The server is currently unavailable (overloaded or down)
The server was acting as a gateway or proxy and did not receive a
504 Gateway Timeout
timely response from the upstream server
505 HTTP Version Not The server does not support the HTTP protocol version used in the
Supported request
511 Network Authentication
The client needs to authenticate to gain network access
Required
Related
ESP8266 (NodeMCU) ADC ESP8266 Arduino WiFi Web ESP8266 Web Server AP (Access
analog value on dial gauge Server LED on o control Point)
February 3, 2018 February 5, 2018 December 16, 2016
In "ESP8266" In "ESP8266" In "IoT Tutorials"
← Upload image (PNG, JPEG) to ESP8266 Web Page and Display it
Installing ESP32 Board in Arduino IDE on Ubuntu Linux →
Leave a Reply
You must be logged in to post a comment.
Login with:
Subscribe to Blog via Recent Posts About Us
Email Best Online JSON Editor Tool to Terms & Conditions
Enter your email address to Edit JSON Online Privacy Policy
subscribe to this blog and Contact Us
ESP8266 IoT Based RGB LED
receive notifications of new
Strip Controller
posts by email.
ESP8266 BMP180 pressure
Join 301 other subscribers sensor interface Search …
ESP8266 weather station using
Email Address Arduino IDE
ESP8266 URL Encode Decode
Example
Subscribe