# ==============================================================================
# Yuitor Smart Classes — Shared Hosting / cPanel public_html .htaccess
# Compatible with Apache 2.4 / LiteSpeed / CloudLinux + Phusion Passenger
# ==============================================================================

Options -Indexes -MultiViews
ServerSignature Off
AddDefaultCharset UTF-8

# Explicit standard MIME types (prevents "Unknown / Miscellaneous" MIME blocks on shared Apache/cPanel hosts & proxies)
<IfModule mod_mime.c>
  AddType application/javascript .js .mjs
  AddType text/css .css
  AddType application/json .json
  AddType application/manifest+json .webmanifest
  AddType image/webp .webp
  AddType image/svg+xml .svg .svgz
  AddType font/woff2 .woff2
  AddType font/woff .woff
</IfModule>

<IfModule mod_rewrite.c>
  RewriteEngine On

  # 1. Force HTTPS (safe behind cPanel / Cloudflare reverse proxies)
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP:X-Forwarded-Proto} !https [NC]
  RewriteCond %{HTTP_HOST} !^(?:localhost|127\.0\.0\.1)(?::\d+)?$ [NC]
  RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

  # 2. Canonical non-www host for yuitor.com (safe: only triggers on www.yuitor.com)
  RewriteCond %{HTTP_HOST} ^www\.yuitor\.com$ [NC]
  RewriteRule ^ https://yuitor.com%{REQUEST_URI} [L,R=301]

  # 3. Block hidden files, private uploads & sensitive runtime directories when extracted in public_html
  RewriteRule ^(?:backups|data|database|logs|scripts|server|storage|vendor|node_modules|dist-shared)(?:/|$) - [F,L,NC]
  RewriteRule ^uploads/(?:resumes|knowledge)(?:/|$) - [F,L,NC]
  RewriteRule (^|/)\.(?!well-known(?:/|$)) - [F,L]
  RewriteRule \.(?:env|git|sql|log|db|sqlite|sqlite3|bak|ts|tsx|map|cjs|sh|yml|yaml)$ - [F,L,NC]
</IfModule>

# Block direct HTTP access to sensitive files even if mod_rewrite is skipped
<FilesMatch "(?i)(^\.(?:env|htaccess|htpasswd|git)|^(?:package(?:-lock)?\.json|tsconfig\.json|server\.(?:js|ts)|app\.cjs|htaccess\.example|readme\.md|deploy-shared-hosting\.md|install-and-links\.txt)$|\.(?:sql|log|db|sqlite|sqlite3|bak|env|sh)$)">
  <IfModule mod_authz_core.c>
    Require all denied
  </IfModule>
  <IfModule !mod_authz_core.c>
    Order allow,deny
    Deny from all
  </IfModule>
</FilesMatch>

# Prevent PHP execution inside public uploads / assets if Apache runs PHP alongside Node
<IfModule mod_php.c>
  php_flag engine off
</IfModule>

# Serve directory index.html cleanly when static folders exist
DirectoryIndex index.html

# ==============================================================================
# Phusion Passenger / Node.js Selector Routing (Layout A recommended)
# ==============================================================================
# When using cPanel "Setup Node.js App":
# - Application Root (yuitor_core): /home/YOUR_CPANEL_USER/yuitor_core
# - Application URL: yuitor.com (cPanel maps this automatically)
# - Startup File: app.cjs
# - Environment variable in cPanel or yuitor_core/.env: YUITOR_PUBLIC_DIR=../public_html
#
# Layout A keeps ONLY public_html/ web-accessible while yuitor_core/ sits
# safely outside public_html.
#
# Layout B (Fallback if your host requires Node inside public_html):
# The RewriteRule & FilesMatch blocks above deny HTTP access to data/, backups/,
# logs/, database/, server/, node_modules/, .env, *.db, *.sql, and *.log.
# ==============================================================================
