如何将 404 重定向到 Mongoose 中的特定 html 文件?
How can I redirect 404 to a specific html file in Mongoose?
我正在尝试使用 struct mg_serve_http_opts 中的选项 url_rewrites:
struct mg_serve_http_opts httpOptions;
httpOptions.url_rewrites = "404=/error.html";
根据 Mongoose 文档,如果我将数字作为 uri_pattern 传递,则 " 它被视为 HTTP 错误代码,并且 file_or_directory_path 应该是要重定向到的 URI "
来源:https://cpr.readthedocs.io/en/latest/opt/mongoose/docs/Options/
但我正在使用 mongoose 6.18,检查源代码,HTTP 错误代码 uri_pattern 似乎已被删除。还有其他方法吗?如果有必要,我也可以升级到 mongoose 7.1,但我真的不想降级到 mongoose 5.6。
谢谢。
我设法为 mongoose 6.18 找到了这个问题的解决方案。它不是像 url_rewrites 这样的通用解决方案,它适用于任何 HTTP 错误代码,但它是一种适用于 HTTP 404 的解决方案。
在事件处理程序中,我必须手动解析 URL 并检查请求的文件是否存在。确实如此,我照常提供 HTTP 页面;如果没有,我提供错误文件。
这是事件处理代码:
static const char *documentRoot = "/var/www/html";
static void eventHandler( struct mg_connection *connection, int event, void *eventData )
{
char resource[4096];
memset( resource, 0, sizeof(resource));
switch( event )
{
case MG_EV_HTTP_REQUEST:
{
struct http_message* httpMessage = (struct http_message*)eventData;
/*
* Build absolute path to the requested file.
*/
strcpy( resource, documentRoot );
strncat( resource, httpMessage->uri.p, httpMessage->uri.len );
/*
* Check if the requested file exists in the filesystem.
*/
struct stat info;
if( stat( resource, &info ) == 0 )
{
struct mg_serve_http_opts httpOptions;
memset( &httpOptions, 0, sizeof(httpOptions));
httpOptions.document_root = documentRoot;
mg_serve_http( connection, httpMessage, httpOptions );
}
else
{
struct mg_str mimeType;
memset( &mimeType, 0, sizeof(mimeType));
struct mg_str extraHeaders;
memset( &extraHeaders, 0, sizeof(extraHeaders));
/*
* Build absolute path to the 404 html file.
*/
strcpy( resource, documentRoot );
strcat( resource, "/error404.html" );
mimeType.p = "text/html";
mimeType.len = strlen( mimeType.p );
mg_http_serve_file( connection, httpMessage, resource, mimeType, extraHeaders );
}
}
break;
default:;
}
}
我正在尝试使用 struct mg_serve_http_opts 中的选项 url_rewrites:
struct mg_serve_http_opts httpOptions;
httpOptions.url_rewrites = "404=/error.html";
根据 Mongoose 文档,如果我将数字作为 uri_pattern 传递,则 " 它被视为 HTTP 错误代码,并且 file_or_directory_path 应该是要重定向到的 URI "
来源:https://cpr.readthedocs.io/en/latest/opt/mongoose/docs/Options/
但我正在使用 mongoose 6.18,检查源代码,HTTP 错误代码 uri_pattern 似乎已被删除。还有其他方法吗?如果有必要,我也可以升级到 mongoose 7.1,但我真的不想降级到 mongoose 5.6。
谢谢。
我设法为 mongoose 6.18 找到了这个问题的解决方案。它不是像 url_rewrites 这样的通用解决方案,它适用于任何 HTTP 错误代码,但它是一种适用于 HTTP 404 的解决方案。
在事件处理程序中,我必须手动解析 URL 并检查请求的文件是否存在。确实如此,我照常提供 HTTP 页面;如果没有,我提供错误文件。
这是事件处理代码:
static const char *documentRoot = "/var/www/html";
static void eventHandler( struct mg_connection *connection, int event, void *eventData )
{
char resource[4096];
memset( resource, 0, sizeof(resource));
switch( event )
{
case MG_EV_HTTP_REQUEST:
{
struct http_message* httpMessage = (struct http_message*)eventData;
/*
* Build absolute path to the requested file.
*/
strcpy( resource, documentRoot );
strncat( resource, httpMessage->uri.p, httpMessage->uri.len );
/*
* Check if the requested file exists in the filesystem.
*/
struct stat info;
if( stat( resource, &info ) == 0 )
{
struct mg_serve_http_opts httpOptions;
memset( &httpOptions, 0, sizeof(httpOptions));
httpOptions.document_root = documentRoot;
mg_serve_http( connection, httpMessage, httpOptions );
}
else
{
struct mg_str mimeType;
memset( &mimeType, 0, sizeof(mimeType));
struct mg_str extraHeaders;
memset( &extraHeaders, 0, sizeof(extraHeaders));
/*
* Build absolute path to the 404 html file.
*/
strcpy( resource, documentRoot );
strcat( resource, "/error404.html" );
mimeType.p = "text/html";
mimeType.len = strlen( mimeType.p );
mg_http_serve_file( connection, httpMessage, resource, mimeType, extraHeaders );
}
}
break;
default:;
}
}