Skip to content

Commit

Permalink
feat: Force HTTPS Redirection
Browse files Browse the repository at this point in the history
  • Loading branch information
reuixiy committed Dec 9, 2019
1 parent 1a8df79 commit 355e0c8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
13 changes: 13 additions & 0 deletions config-examples/en-us/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,19 @@ uglyURLs = false
# by MemE only


######################################
# Force HTTPS Redirection

# Note: render only in production
# environment

enableForceHTTPS = false
# Note: use JavaScript for redirection.
# You can enable this if your
# server does not support forced
# HTTPS.


######################################
# 404 Page

Expand Down
12 changes: 12 additions & 0 deletions config-examples/zh-cn/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,18 @@ uglyURLs = false
# 说明:仅对 MemE 生成的 CSS 和 JS 有效


######################################
# HTTPS 强制跳转

# 说明:仅在生产环境(production)下渲染

# 是否开启
enableForceHTTPS = false
# 说明:使用 JavaScript 来实现跳转。如果你
# 的服务器不支持强制 HTTPS 跳转,则可
# 开启此选项。


######################################
# 404 页面

Expand Down
14 changes: 13 additions & 1 deletion layouts/partials/head.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<head {{ if .Site.Params.openGraph }}prefix="og: http://ogp.me/ns# article: http://ogp.me/ns/article#"{{ end }}>
<meta charset="UTF-8" />

{{ $themeColor := .Site.Params.themeColor -}}
{{- $themeColorDark := .Site.Params.themeColorDark -}}
{{- with .Site.Params.defaultTheme | default "light" -}}
Expand All @@ -23,7 +25,17 @@
</script>
{{- end }}

<meta charset="UTF-8" />
<!-- Force HTTPS Redirection -->
{{- if and .Site.Params.enableForceHTTPS (eq hugo.Environment "production") }}
{{ $url := urls.Parse .Site.BaseURL }}
{{ $host := $url.Host }}
<script>
if (window.location.host == "{{ $host | safeJS }}" && window.location.protocol != "https:") {
window.location.protocol = "https";
}
</script>
{{- end }}

<!-- Responsive Web Design -->
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=2.0, user-scalable=yes" />
<!-- Old Old IE -->
Expand Down

1 comment on commit 355e0c8

@reuixiy
Copy link
Owner Author

@reuixiy reuixiy commented on 355e0c8 Dec 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.