# ----------------------------------------------------------------------
# Better website experience for IE users
# ----------------------------------------------------------------------

# Force the latest IE version, in various cases when it may fall back to IE7 mode
#  github.com/rails/rails/commit/123eb25#commitcomment-118920
# Use ChromeFrame if it's installed for a better experience for the poor IE folk

<IfModule mod_headers.c>
  Header set X-UA-Compatible "IE=Edge,chrome=1"
  # mod_headers can't match by content-type, but we don't want to send this header on *everything*...
  <FilesMatch "\.(js|css|gif|png|jpe?g|pdf|xml|oga|ogg|m4a|ogv|mp4|m4v|webm|svg|svgz|eot|ttf|otf|woff|woff2|ico|webp|appcache|manifest|htc|crx|oex|xpi|safariextz|vcf)$" >
    Header unset X-UA-Compatible
  </FilesMatch>
</IfModule>

# ----------------------------------------------------------------------
# Proper MIME type for all files
# ----------------------------------------------------------------------

# JavaScript
#   Normalize to standard type (it's sniffed in IE anyways)
#   tools.ietf.org/html/rfc4329#section-7.2
AddType application/javascript         js jsonp
AddType application/json               json

# Audio
AddType audio/ogg                      oga ogg
AddType audio/mp4                      m4a f4a f4b

# Video
AddType video/ogg                      ogv
AddType video/mp4                      mp4 m4v f4v f4p
AddType video/webm                     webm
AddType video/x-flv                    flv

# SVG
#   Required for svg webfonts on iPad
#   twitter.com/FontSquirrel/status/14855840545
AddType     image/svg+xml              svg svgz
AddEncoding gzip                       svgz

# Webfonts
AddType application/vnd.ms-fontobject  eot
AddType application/x-font-ttf         ttf ttc
AddType font/opentype                  otf
AddType application/x-font-woff        woff
AddType application/font-woff2         woff2

# Assorted types
AddType image/x-icon                        ico
AddType image/webp                          webp
AddType text/cache-manifest                 appcache manifest
AddType text/x-component                    htc
AddType application/xml                     rss atom xml rdf
AddType application/x-chrome-extension      crx
AddType application/x-opera-extension       oex
AddType application/x-xpinstall             xpi
AddType application/octet-stream            safariextz
AddType application/x-web-app-manifest+json webapp
AddType text/x-vcard                        vcf
AddType application/x-shockwave-flash       swf
AddType text/vtt                            vtt


# ----------------------------------------------------------------------
# Gzip compression
# ----------------------------------------------------------------------

<IfModule mod_deflate.c>
  # Force deflate for mangled headers developer.yahoo.com/blogs/ydn/posts/2010/12/pushing-beyond-gzipping/
  <IfModule mod_setenvif.c>
    <IfModule mod_headers.c>
      SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
      RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
    </IfModule>
  </IfModule>

  # Compress all output labeled with one of the following MIME-types
  <IfModule mod_filter.c>
    AddOutputFilterByType DEFLATE application/atom+xml \
                                  application/javascript \
                                  application/json \
                                  application/rss+xml \
                                  application/vnd.ms-fontobject \
                                  application/x-font-ttf \
                                  application/xhtml+xml \
                                  application/xml \
                                  font/opentype \
                                  image/svg+xml \
                                  image/x-icon \
                                  text/css \
                                  text/html \
                                  text/plain \
                                  text/x-component \
                                  text/xml
  </IfModule>
</IfModule>

# ----------------------------------------------------------------------
# Start rewrite engine
# ----------------------------------------------------------------------

# Turning on the rewrite engine is necessary for the following rules and
# features. FollowSymLinks must be enabled for this to work.

# Some cloud hosting services require RewriteBase to be set: goo.gl/HOcPN
# If using the h5bp in a subdirectory, use `RewriteBase /foo` instead where
# 'foo' is your directory.

# If your web host doesn't allow the FollowSymlinks option, you may need to
# comment it out and use `Options +SymLinksOfOwnerMatch`, but be aware of the
# performance impact: http://goo.gl/Mluzd

<IfModule mod_rewrite.c>
  <Directory "${HOME}">

    Options Indexes FollowSymLinks
    AllowOverride All
    Order Allow,Deny
    Allow from All

    RewriteEngine On
    RewriteBase /

    # Rules have NE added to the end to preserve either explicit or implicit # anchor tags

    # set OKD docs minishift to go to the 3.11 branch as minishift is not on 4.x (yet)
    RewriteRule ^latest/minishift/?(.*)$ /3.11/minishift/$1 [NE,R=301]

    # Redirects for "latest" version
    RewriteRule ^(origin-m4)/?(.*)$ /latest/$2 [R=301]

    # Welcome page redirects
    RewriteRule ^(latest|[0-9.]+)/?$ /$1/welcome/index.html [L,R=301]

    # Overview page has moved to Index page
    RewriteRule ^(.*)/overview\.html(.*)$ /$1/index.html$2 [NE,L,R=301]

    # Developers console redirect
    RewriteRule ^(latest|[0-9.]+)/getting_started/developers/developers_console\.html(.*)$ /$1/getting_started/developers_console.html$2 [NE,L,R=301]

    # Deployments redirect
    RewriteRule ^(latest|[0-9.]+)/dev_guide/deployments\.html(.*)$ /$1/dev_guide/deployments/how_deployments_work.html$2 [NE,L,R=301]

    # Builds redirect
    RewriteRule ^(latest|[0-9.]+)/dev_guide/builds\.html(.*)$ /$1/dev_guide/builds/index.html$2 [NE,L,R=301]

    # Install Guide redirect
    RewriteRule ^latest/install_config/install/advanced_install\.html(.*)$ /latest/install/index.html$1 [NE,L,R=301]

    # Other specific redirects
    RewriteRule ^latest/admin_guide/build_defaults_overrides\.html(.*)$ /latest/install_config/build_defaults_overrides.html$1 [NE,L,R=301]
    RewriteRule ^latest/admin_guide/install/(advanced_install|deploy_router|docker_registry|first_steps|overview|prerequisites|quick_install|upgrades)\.html(.*)$ /latest/install/$1.html$2 [NE,R=301]
    RewriteRule ^(latest|[0-9.]+)/admin_guide/upgrades\.html(.*) /$1/install_config/upgrades.html$2 [NE,R=301]
    RewriteRule ^(latest|[0-9.]+)/admin_guide/(aggregate_logging|cluster_metrics|configuring_authentication|configuring_aws|configuring_gce|configuring_openstack|http_proxies|master_node_configuration|native_container_routing|routing_from_edge_lb|syncing_groups_with_ldap|web_console_customization)\.html(.*)$ /$1/install_config/$2.html$3 [NE,R=301]
    RewriteRule ^(latest|[0-9.]+)/admin_guide/persistent_storage/(persistent_storage_aws|persistent_storage_cinder|persistent_storage_gce|persistent_storage_glusterfs|persistent_storage_nfs)\.html(.*)$ /$1/install_config/persistent_storage/$2.html$3 [NE,R=301]
    RewriteRule ^latest/admin_guide/selfprovisioned_projects\.html(.*)$ /$1/$2/admin_guide/managing_projects.html$3 [NE,R=301]
    RewriteRule ^latest/install_config/upgrades\.html(.*)$ /latest/install_config/upgrading/index.html$1 [NE,R=301]
    RewriteRule ^latest/install_config/upgrading/(.*)$ /latest/upgrading/$1 [NE,R=301]

    # The following rule prevents an infinite redirect loop when browsing to /(latest|4\.16)/virt/about_virt/about-virt.html
    #RewriteRule ^(latest|4\.16)/virt/about_virt/about-virt.html$ - [L]

    # OpenShift Virtualization (CNV) catchall redirect; use when CNV releases asynchronously from OCP. Do not change the 302 to a 301.
    # When uncommented, this redirects all `virt` directory traffic to the about-virt page.
    # Pay mind to the redirect directly above this which prevents redirect loops.
    # To activate the redirects, uncomment the next and previous lines and update the version number to the pending release.
    #RewriteRule ^(latest|4\.16)/virt/(?!about-virt\.html)(.+)$ /$1/virt/about_virt/about-virt.html [NE,R=302]

    # Red Hat OpenShift support for Windows Containers (WMCO) catchall redirect; use when WMCO releases asynchronously from OCP. Do not change the 302 to a 301.
    # When uncommented, this redirects all `windows_containers` directory traffic to the /windows_containers/index.html page.
    # To activate the redirects, uncomment the next line and update the version number to the pending release.
    # RewriteRule ^(latest|4\.15)/windows_containers/(?!index\.html)(.+)$ /$1/windows_containers/index.html [NE,R=302]
    # RewriteRule ^(latest|4\.15)/support/troubleshooting/troubleshooting-windows-container-workload-issues\.html(.*)$ /$1/windows_containers/index.html [NE,R=302]

  </Directory>
</IfModule>

# ----------------------------------------------------------------------
# Prevent 404 errors for non-existing redirected folders
# ----------------------------------------------------------------------

# without -MultiViews, Apache will give a 404 for a rewrite if a folder of the
# same name does not exist.
# webmasterworld.com/apache/3808792.htm

Options -MultiViews

# ----------------------------------------------------------------------
# Custom 404 page
# ----------------------------------------------------------------------

# You can add custom pages to handle 500 or 403 pretty easily, if you like.
# If you are hosting your site in subdirectory, adjust this accordingly
#    e.g. ErrorDocument 404 /subdir/404.html
ErrorDocument 404 /404.html


# ----------------------------------------------------------------------
# UTF-8 encoding
# ----------------------------------------------------------------------

# Use UTF-8 encoding for anything served text/plain or text/html
AddDefaultCharset utf-8

# Force UTF-8 for a number of file formats
AddCharset utf-8 .atom .css .js .json .rss .vtt .xml


# ----------------------------------------------------------------------
# A little more security
# ----------------------------------------------------------------------

# "-Indexes" will have Apache block users from browsing folders without a
# default document Usually you should leave this activated, because you
# shouldn't allow everybody to surf through every folder on your server (which
# includes rather private places like CMS system folders).
<IfModule mod_autoindex.c>
  Options -Indexes
</IfModule>

# Block access to "hidden" directories or files whose names begin with a
# period. This includes directories used by version control systems such as
# Subversion or Git.
<IfModule mod_rewrite.c>
  RewriteCond %{SCRIPT_FILENAME} -d [OR]
  RewriteCond %{SCRIPT_FILENAME} -f
  RewriteRule "(^|/)\." - [F]
</IfModule>

# Block access to backup and source files. These files may be left by some
# text/html editors and pose a great security danger, when anyone can access
# them.
<FilesMatch "(\.(bak|config|sql|fla|psd|ini|log|sh|inc|swp|dist)|~)$">
  Order allow,deny
  Deny from all
  Satisfy All
</FilesMatch>
