Skip to content

Instantly share code, notes, and snippets.

@aktaumag
Created May 15, 2023 13:50
Show Gist options
  • Save aktaumag/b06f762fa60bd4e04cc85d975a80bd1d to your computer and use it in GitHub Desktop.
Save aktaumag/b06f762fa60bd4e04cc85d975a80bd1d to your computer and use it in GitHub Desktop.
Настройка кода ответа сервера и отображения
Настройка кода ответа сервера и отображения
@aktaumag
Copy link
Author

aktaumag commented May 15, 2023

Нужно, чтоб любой не существующий URI адрес сайта отдавал корректную страницу об 404 ошибке с 404 кодом ответа сервера

PHP код для добавления 404 ответа сервера:

header( "HTTP/1.1 404 Not Found");

//или
header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found");

//или
http_response_code(404); (для использования сообщения по умолчанию)

//проверяем код ответа сервера:
if(http_response_code() == 404) { echo "Страница не найдена"; }

Настройка в файле .htaccess для отображения собственной страницы 404.

ErrorDocument 404 /404.html

Настройка в конфигурационном файле NGINX для отображения собственной страницы 404.

error_page 404 /404.html;
location = /404.html {
    root /usr/share/nginx/html;
    internal;
}

@aktaumag
Copy link
Author

aktaumag commented May 15, 2023

Желательно делать в стиле сайта.
Хорошо, если будет шапка с логотипом, подвал с навигационными ссылками, форма поиска по сайту, тематическая смешная картинка и прочее.

Ну или как минимум взять любую подходящую html версию простенькой страницы 404 и переделать под стиль сайта!

Вот припер:

<!DOCTYPE html>
<html lang="ru-ru" dir="ltr">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>404 - Страница не найдена</title>

    <!-- Custom stlyle -->
<style>
* {
  -webkit-box-sizing: border-box;
          box-sizing: border-box;
}

body {
  padding: 0;
  margin: 0;
    background-image: url(../img/main_bg.jpg);
}

#notfound {
  position: relative;
  height: 100vh;
}

#notfound .notfound {
  position: absolute;
  left: 50%;
  top: 50%;
  -webkit-transform: translate(-50%, -50%);
      -ms-transform: translate(-50%, -50%);
          transform: translate(-50%, -50%);
}

.notfound {
  max-width: 710px;
  width: 100%;
  padding-left: 190px;
  line-height: 1.4;
}

.notfound .notfound-404 {
  position: absolute;
  left: 0;
  top: 0;
  width: 150px;
  height: 150px;
}

.notfound .notfound-404 h1 {
  font-family: 'Passion One', cursive;
  color: #007888;
  font-size: 150px;
  letter-spacing: 15.5px;
  margin: 0px;
  font-weight: 900;
  position: absolute;
  left: 50%;
  top: 50%;
  -webkit-transform: translate(-50%, -50%);
      -ms-transform: translate(-50%, -50%);
          transform: translate(-50%, -50%);
}

.notfound h2 {
  font-family: 'Oswald', sans-serif;
    color: #007888;
  font-size: 28px;
    font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 2.5px;
  margin-top: 0;
}

.notfound p {
  font-family: 'Oswald', sans-serif;
  margin-top: 0;
    color: #383232;
    font-size: 18px;
    font-weight: 400;
    line-height: 25px;
    margin-bottom: 26px;
}

.notfound a {
    font-family: 'Oswald',sans-serif;
  font-size: 14px;
  text-decoration: none;
  text-transform: uppercase;
  background: #fff;
  display: inline-block;
  padding: 15px 30px;
  border-radius: 40px;
  color: #292929;
  font-weight: 700;
  -webkit-box-shadow: 0px 4px 15px -5px rgba(0, 0, 0, 0.3);
          box-shadow: 0px 4px 15px -5px rgba(0, 0, 0, 0.3);
  -webkit-transition: 0.2s all;
  transition: 0.2s all;
}

.notfound a:hover {
  color: #fff;
  background-color: #007888;
}

@media only screen and (max-width: 480px) {
  .notfound {
    text-align: center;
  }
  .notfound .notfound-404 {
    position: relative;
    width: 100%;
    margin-bottom: 15px;
  }
  .notfound {
    padding-left: 15px;
    padding-right: 15px;
  }
}
</style>
    <!-- Google font -->
    <link href="https://fonts.googleapis.com/css?family=Oswald:400,500,700&display=swap&subset=cyrillic" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Passion+One:900" rel="stylesheet">
</head>
<body>

    <div id="notfound">
        <div class="notfound">
            <div class="notfound-404">
                <h1>:(</h1>
            </div>
            <h2>404 - Страница не найдена</h2>
	        
                            <p>Страница, которую вы ищете, возможно была удалена, изменилось ее имя или просто временно недоступна.</p>
                        
            <a href="/" title="Вернуться на Главную страницу">Главная страница</a>
        </div>
        
	         
    </div>

</body>
</html>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment