# University Dental Centre – production .htaccess (hardened 2026-09)
DirectoryIndex index.php
Options -Indexes -MultiViews
ServerSignature Off

# ---------------------------------------------------------------- PHP whitelist
# Only the site's own entry points may run. A webshell uploaded under any other
# name (e.g. /wp-conf.php, /img/x.php, /about1.php) gets 403 instead of executing.
# When the developer adds a NEW .php page, add its name here.
<FilesMatch "(?i)\.(php\d?|phtml|pht|phar|inc|cgi|pl|py|sh|asp|aspx|jsp)$">
  Require all denied
</FilesMatch>
<FilesMatch "^(index|404|contact|service|services|health|sitemap|post|appointment|aligners|cosmetic-dentistry|custom-dentures|dental-checkup|dental-crown|dental-implants|emergency-dental-care|filling|general-dentistry|gum-care|hollywood-smile|laser-dentistry|orthodontics-braces|pediatric-dentistry|retainers|root-canal|smile-makeover|teeth-whitening|tooth-extraction|veneers|login|logout|account|appointments|blog|blog-edit|content|doctors|doctor-edit|errors|events|security|service-edit|settings|uptime|users|visitors)\.php$">
  Require all granted
</FilesMatch>

# ------------------------------------------------------ never serve these files
# Dotfiles (.htaccess, .env, .git, .user.ini …); /.well-known/ files are unaffected
<FilesMatch "^\.">
  Require all denied
</FilesMatch>
<FilesMatch "(?i)^(config\.php|secrets\.php|composer\.(json|lock)|package(-lock)?\.json|php\.ini|web\.config|error_log|php_errorlog|desktop\.ini|thumbs\.db)$">
  Require all denied
</FilesMatch>
# Backups, archives, dumps, logs, documents – attackers scan for these
<FilesMatch "(?i)\.(zip|rar|7z|tar|gz|tgz|bz2|sql|bak|backup|old|orig|save|swp|tmp|log|md|docx?|xlsx?|db|sqlite|ini|env|pem|key|crt|dist|example|exe|dll|bat|cmd)$">
  Require all denied
</FilesMatch>
# Known malware / CMS probe names
<FilesMatch "(?i)^(xmlrpc|wp-login|wp-config|adminfuns|cjfuns|classsmtps|system_log|inputs|shell|c99|r57|wso|alfa|mini|up|uploader|cmd|test|test_ver|info|phpinfo|user)\.php$">
  Require all denied
</FilesMatch>

<IfModule mod_headers.c>
  Header always set X-Content-Type-Options "nosniff"
  Header always set X-Frame-Options "SAMEORIGIN"
  Header always set Referrer-Policy "strict-origin-when-cross-origin"
  Header always set Permissions-Policy "geolocation=(), microphone=(), camera=(), payment=(), usb=()"
  Header always set Content-Security-Policy "frame-ancestors 'self'; base-uri 'self'; object-src 'none'"
  Header always set Strict-Transport-Security "max-age=31536000" "expr=%{HTTPS} == 'on'"
  Header always unset X-Powered-By
  Header unset X-Powered-By
</IfModule>

# Missing URLs are routed to 404.php by the rewrite rule below (real 404 status,
# works both at the domain root and in a local subfolder).

<IfModule mod_rewrite.c>
  RewriteEngine On

  # Block HTTP methods the site never uses
  RewriteCond %{REQUEST_METHOD} !^(GET|POST|HEAD)$ [NC]
  RewriteRule ^ - [F]

  # Basic filter for common exploit payloads in the query string
  RewriteCond %{QUERY_STRING} (base64_(en|de)code|eval\(|GLOBALS(=|\[)|_REQUEST(=|\[)|<script|%3Cscript|\.\./|%2e%2e%2f|/etc/passwd|php://|data://|expect://|union.*select|concat\() [NC]
  RewriteRule ^ - [F]

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

  # WWW → non-www
  RewriteCond %{HTTP_HOST} ^www\.(dentalcentrealain\.com)$ [NC]
  RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]

  # Old website backup folder – must not exist on the server
  RewriteRule ^old-php(/|$) - [R=404,L]

  # Live sitemap (includes services/blog posts added in admin)
  RewriteRule ^sitemap\.xml$ sitemap.php [L]

  # Existing files/dirs pass through (blog, img, assets, api)
  RewriteCond %{REQUEST_FILENAME} -f [OR]
  RewriteCond %{REQUEST_FILENAME} -d
  RewriteRule ^ - [L]

  # Language homepages already physical: /en/ /ar/ /ru/

  # Blog posts: /blog/my-post → blog/post.php?slug=my-post
  RewriteRule ^blog/([a-z0-9-]+)/?$ blog/post.php?slug=$1 [L,QSA]

  # Services added in admin (no physical file): /my-service.php → service.php
  RewriteRule ^([a-z0-9-]+)\.php$ service.php?slug=$1 [L,QSA]

  # Everything else → localized 404 page
  RewriteRule ^ 404.php [L]
</IfModule>
