# ---------------------------
# BASIC AUTH
# ---------------------------
# Satisfy Any
# AuthType Basic
# AuthName "Input your ID and Password."
# AuthUserFile /var/www/html/.htpasswd
# require valid-user

# ---------------------------
# ログインページをIPアドレス制限
# ---------------------------
# SetEnvIf Request_URI "(/login/)" login
# Order Deny,Allow
# Deny from env=login
# Allow from IP xxx.xxx.xxx.xxx

# ---------------------------
# php 設定
# ---------------------------
# php_value max_input_vars 99999
# php_value memory_limit 512M
# php_value post_max_size 64M
# php_value upload_max_filesize 16M

# ---------------------------
# 全体のIPアドレス制限
# ---------------------------
# <RequireAll>
#   Require not ip xxx.xxx.xxx.xxx
#   Require not ip yyy.yyy.yyy.yyy
#   Require all granted
# </RequireAll>

# ---------------------------------------
# index.php以外のPHPスクリプトを実行禁止
# ---------------------------------------
<FilesMatch "\.php$">
  Require all denied
</FilesMatch>
<Files "index.php">
  Require all granted
</Files>

# ---------------------------------------
# 隠しファイル（ドットファイル）へのアクセス禁止
# ---------------------------------------
<Files ~ "^\.">
  Require all denied
</Files>

# ---------------------------------------
# config.server.php へのアクセス禁止
# ---------------------------------------
<Files config.server.php>
  Require all denied
</Files>

# ---------------------------------
# 静的アセットのセキュリティ設定
# ---------------------------------
<FilesMatch "\.(js|css|jpg|jpeg|png|gif|webp|svg|woff|woff2|ttf|eot|ico)$">
  Header set X-Content-Type-Options "nosniff"
  Header set X-Frame-Options "SAMEORIGIN"
  Header set Referrer-Policy "strict-origin-when-cross-origin"
  Header set X-XSS-Protection "1; mode=block"
  Header set Content-Security-Policy "default-src 'self';"
  Header set Strict-Transport-Security "max-age=63072000; includeSubDomains;"
</FilesMatch>

# ---------------------------------
# 静的アセットのキャッシュ設定
# ---------------------------------
<Files ~ "\.(css|js)$">
  <IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 365 days"
  </IfModule>
  <IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE application/javascript
  </IfModule>
  <IfModule mod_headers.c>
    Header set Cache-Control "public, max-age=31536000, immutable"
  </IfModule>
  FileETag None
</Files>
<Files ~ "\.(jpg|jpeg|png|gif|webp|svg|woff|woff2|ttf|eot|ico)$">
  <IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 365 days"
  </IfModule>
  <IfModule mod_headers.c>
    Header unset ETag
    Header set Cache-Control "public, max-age=31536000, immutable"
  </IfModule>
  FileETag None
</Files>

# ---------------------------
# Contents Security Policy
# ---------------------------
# <IfModule mod_headers.c>
#   Header set Content-Security-Policy " \
#     default-src \
#       'self'; \
#     style-src \
#       'self' \
#       'unsafe-inline' \
#       *.googleapis.com \
#       *.google.com; \
#     script-src \
#       'self' \
#       'unsafe-inline' \
#       'unsafe-eval' \
#       *.google.com \
#       *.googletagmanager.com \
#       *.google-analytics.com \
#       *.googleapis.com \
#       *.googleadservices.com; \
#     img-src \
#       'self' \
#       data: \
#       *.google.com; \
#     frame-src \
#       'self' \
#       *.google.com; \
#     frame-ancestors \
#       'self'; \
#     block-all-mixed-content; \
#   "
# </IfModule>

# ---------------------------
# URL書き換え設定
# ---------------------------
RewriteEngine on
# RewriteBase /
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# ------------------------------------------------------------------
# 実行可能なスクリプト (PHP, CGI, Perl, Python, Shell, EXE) をブロック
# ------------------------------------------------------------------
# RewriteCond %{REQUEST_URI} !^/index\.php$ [NC]
# RewriteCond %{REQUEST_URI} !^/_setup/.*\.php$ [NC]
# RewriteCond %{REQUEST_URI} !^/other2/index\.php$ [NC]
# RewriteCond %{REQUEST_URI} \.(php|cgi|pl|py|sh|exe|bin|dll|bat|cmd|msi|jar)$ [NC]
# RewriteRule ^.*$ - [F,L]

# ---------------------------
# webpフォーマット画像対応
# ---------------------------
RewriteCond %{HTTP_ACCEPT} image/webp
RewriteCond %{REQUEST_FILENAME} (.*)\.(jpe?g|png)$
RewriteCond %{REQUEST_FILENAME}\.webp -f
RewriteCond %{QUERY_STRING} !type=original
RewriteRule (.+)\.(jpe?g|png)$ %{REQUEST_FILENAME}.webp [T=image/webp,E=accept:1]
<IfModule mod_headers.c>
    Header append Vary Accept env=REDIRECT_accept
</IfModule>
<IfModule mod_mime.c>
    AddType image/webp .webp
</IfModule>

# ---------------------------
# wwwあり、なし正規化
# ---------------------------
# RewriteCond %{HTTP_HOST} ^(example\.com)(:80)?
# RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,NE,L]

# -----------------------------------------
# 存在するファイルへの直指定の場合、CMSを通さない
# -----------------------------------------
# RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .* - [L]

# ---------------------------
# index.html なしへの正規化
# ---------------------------
RewriteCond %{REQUEST_URI} ^(.*)/index.html$
RewriteRule .* https://%{HTTP_HOST}%1/ [R=301,NE,L]

# ---------------------------
# 最後にスラッシュをつける
# ---------------------------
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !\.[^/\.]+$
RewriteRule .* %{REQUEST_URI}/ [R=301,L]

# -----------------------------------
# 拡張子がある場合、最後のスラッシュをとる
# -----------------------------------
# RewriteCond %{REQUEST_URI} \.[^/\.]+/$
# RewriteRule ^(.*)/$ https://%{HTTP_HOST}/$1 [L,NE,R=301]

# ------------------------------------------------------------------
# メンテナンスモード時に503ページを出力 (setup_は環境に合わせて変更して下さい)
# ------------------------------------------------------------------
# RewriteRule ^setup_/ - [L]
# RewriteRule ^(.*)$ setup_/503.php [L]

# ------------------------------------------------------------------
# a-blog cms以外のコンテンツ（a-blog cmsを動作させないコンテンツ）
# ------------------------------------------------------------------
# RewriteCond %{REQUEST_URI} !^/?other/
# RewriteCond %{REQUEST_URI} !^/?other2/
RewriteCond %{REQUEST_URI} !/js/locales/(.*)/translation.json

# ---------------------------
# CMSを実行
# ---------------------------
RewriteRule . index.php [L]
