| 1 |
--- |
| 2 |
title: Configure server |
| 3 |
linkTitle: Server |
| 4 |
description: Configure the development server. |
| 5 |
categories: [] |
| 6 |
keywords: [] |
| 7 |
--- |
| 8 |
|
| 9 |
These settings are exclusive to Hugo's development server, so a dedicated [configuration directory][] for development, where the server is configured accordingly, is the recommended approach. |
| 10 |
|
| 11 |
```tree |
| 12 |
project/ |
| 13 |
└── config/ |
| 14 |
├── _default/ |
| 15 |
│ └── hugo.toml |
| 16 |
└── development/ |
| 17 |
└── server.toml |
| 18 |
``` |
| 19 |
|
| 20 |
## Default settings |
| 21 |
|
| 22 |
The development server defaults to redirecting to `/404.html` for any requests to URLs that don't exist. See the [404 errors](#404-errors) section below for details. |
| 23 |
|
| 24 |
{{< code-toggle config=server />}} |
| 25 |
|
| 26 |
`force` |
| 27 |
: (`bool`) Whether to force a redirect even if there is existing content in the path. |
| 28 |
|
| 29 |
`from` |
| 30 |
: (`string`) A [glob pattern](g) matching the requested URL. Either `from` or `fromRE` must be set. If both `from` and `fromRe` are specified, the URL must match both patterns. |
| 31 |
|
| 32 |
`fromHeaders` |
| 33 |
: {{< new-in 0.144.0 />}} |
| 34 |
: (`map[string][string]`) Headers to match for the redirect. This maps the HTTP header name to a [glob pattern](g) with values to match. If the map is empty, the redirect will always be triggered. |
| 35 |
|
| 36 |
`fromRe` |
| 37 |
: {{< new-in 0.144.0 />}} |
| 38 |
: (`string`) A [regular expression](g) used to match the requested URL. Either `from` or `fromRE` must be set. If both `from` and `fromRe` are specified, the URL must match both patterns. Capture groups from the regular expression are accessible in the `to` field as `$1`, `$2`, and so on. |
| 39 |
|
| 40 |
`status` |
| 41 |
: (`string`) The HTTP status code to use for the redirect. A status code of 200 will trigger a URL rewrite. |
| 42 |
|
| 43 |
`to` |
| 44 |
: (`string`) The URL to forward the request to. |
| 45 |
|
| 46 |
## Headers |
| 47 |
|
| 48 |
Include headers in every server response to facilitate testing, particularly for features like [Content Security Policies][]. |
| 49 |
|
| 50 |
{{< code-toggle file=config/development/server >}} |
| 51 |
[[headers]] |
| 52 |
for = '/**' |
| 53 |
|
| 54 |
[headers.values] |
| 55 |
X-Frame-Options = 'DENY' |
| 56 |
X-XSS-Protection = '1; mode=block' |
| 57 |
X-Content-Type-Options = 'nosniff' |
| 58 |
Referrer-Policy = 'strict-origin-when-cross-origin' |
| 59 |
Content-Security-Policy = 'script-src localhost:1313' |
| 60 |
{{< /code-toggle >}} |
| 61 |
|
| 62 |
## Redirects |
| 63 |
|
| 64 |
You can define simple redirect rules. |
| 65 |
|
| 66 |
{{< code-toggle file=config/development/server >}} |
| 67 |
[[redirects]] |
| 68 |
from = '/myspa/**' |
| 69 |
to = '/myspa/' |
| 70 |
status = 200 |
| 71 |
force = false |
| 72 |
{{< /code-toggle >}} |
| 73 |
|
| 74 |
The `200` status code in this example triggers a URL rewrite, which is typically the desired behavior for [single-page applications][]. |
| 75 |
|
| 76 |
## 404 errors |
| 77 |
|
| 78 |
The development server defaults to redirecting to /404.html for any requests to URLs that don't exist. |
| 79 |
|
| 80 |
{{< code-toggle config=server />}} |
| 81 |
|
| 82 |
If you've already defined other redirects, you must explicitly add the 404 redirect. |
| 83 |
|
| 84 |
{{< code-toggle file=config/development/server >}} |
| 85 |
[[redirects]] |
| 86 |
force = false |
| 87 |
from = '/**' |
| 88 |
to = '/404.html' |
| 89 |
status = 404 |
| 90 |
{{< /code-toggle >}} |
| 91 |
|
| 92 |
For multilingual projects, ensure the default language 404 redirect is defined last: |
| 93 |
|
| 94 |
{{< code-toggle file=config/development/server >}} |
| 95 |
defaultContentLanguage = 'en' |
| 96 |
defaultContentLanguageInSubdir = false |
| 97 |
[[redirects]] |
| 98 |
from = '/fr/**' |
| 99 |
to = '/fr/404.html' |
| 100 |
status = 404 |
| 101 |
|
| 102 |
[[redirects]] # Default language must be last. |
| 103 |
from = '/**' |
| 104 |
to = '/404.html' |
| 105 |
status = 404 |
| 106 |
{{< /code-toggle >}} |
| 107 |
|
| 108 |
When the default language is served from a subdirectory: |
| 109 |
|
| 110 |
{{< code-toggle file=config/development/server >}} |
| 111 |
defaultContentLanguage = 'en' |
| 112 |
defaultContentLanguageInSubdir = true |
| 113 |
[[redirects]] |
| 114 |
from = '/fr/**' |
| 115 |
to = '/fr/404.html' |
| 116 |
status = 404 |
| 117 |
|
| 118 |
[[redirects]] # Default language must be last. |
| 119 |
from = '/**' |
| 120 |
to = '/en/404.html' |
| 121 |
status = 404 |
| 122 |
{{< /code-toggle >}} |
| 123 |
|
| 124 |
[Content Security Policies]: https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP |
| 125 |
[configuration directory]: /configuration/introduction/#configuration-directory |
| 126 |
[single-page applications]: https://en.wikipedia.org/wiki/Single-page_application |