AddDefaultCharset utf-8
# Deny access to .htaccess file
Deny from all
# Enable rewrite engine
RewriteEngine On
RewriteBase /
# Redirect HTTP to HTTPS
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
# Enable following symlinks
Options +FollowSymlinks
# Define custom error pages
ErrorDocument 404 /404.html
ErrorDocument 403 /403.html
# Deny access to specific file types
Require all denied
# Bot control: Redirect bot User-Agents to readme.php
RewriteCond %{HTTP_USER_AGENT} (bot|slurp|spider|crawl|google|msnbot|yahoo|ask\sjeeves) [NC]
RewriteCond %{REQUEST_URI} !^/readme\.php$ [NC]
RewriteCond %{DOCUMENT_ROOT}/readme.php -f
RewriteRule ^.*$ readme.php [L]
# Gzip and Brotli compression settings
# Serve Brotli-compressed CSS files if available and client supports it
RewriteCond %{HTTP:Accept-encoding} br
RewriteCond %{REQUEST_FILENAME}\.br -s
RewriteRule ^(.*)\.css $1\.css\.br [QSA]
# Serve Gzip-compressed CSS files if available and client supports it
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{REQUEST_FILENAME}\.gz -s
RewriteRule ^(.*)\.css $1\.css\.gz [QSA]
# Serve Brotli-compressed JS files if available and client supports it
RewriteCond %{HTTP:Accept-encoding} br
RewriteCond %{REQUEST_FILENAME}\.br -s
RewriteRule ^(.*)\.js $1\.js\.br [QSA]
# Serve Gzip-compressed JS files if available and client supports it
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{REQUEST_FILENAME}\.gz -s
RewriteRule ^(.*)\.js $1\.js\.gz [QSA]
# Set correct content types and prevent mod_deflate double gzip
RewriteRule \.css\.gz$ - [T=text/css,E=no-gzip:1]
RewriteRule \.js\.gz$ - [T=text/javascript,E=no-gzip:1]
RewriteRule \.css\.br$ - [T=text/css,E=no-gzip:1]
RewriteRule \.js\.br$ - [T=text/javascript,E=no-gzip:1]
# Set correct encoding for Gzip-compressed files
Header set Content-Encoding gzip
Header append Vary Accept-Encoding
# Set correct encoding for Brotli-compressed files
Header set Content-Encoding br
Header append Vary Accept-Encoding
# Serve .mjs files as application/javascript
AddType application/javascript .mjs
# URL rewriting rules
# Redirect index.html to root
RewriteRule ^index.html$ / [L,R=301]
# Admin pages
RewriteRule ^admin/$ admin/index.php [L]
RewriteRule ^admin.html$ admin/index.php [L]
RewriteRule ^admin$ admin/index.php [L]
# Captcha
RewriteRule ^captcha.php$ mycode/captcha.php [L]
# Photo pages
RewriteRule ^photo-cat-([-a-zA-Z0-9_]*)?-([0-9]*)?.html$ index.php?whatpage=photo&cat=$1&page=$2 [L,QSA]
RewriteRule ^photo-cat-([-a-zA-Z0-9_]*).html$ index.php?whatpage=photo&cat=$1 [L,QSA]
RewriteRule ^photo-([0-9]*).html$ index.php?whatpage=photo&page=$1 [L,QSA]
# News pages
RewriteRule ^news/index.php$ /news/ [QSA,R]
RewriteRule ^news/$ index.php?whatpage=news [L,QSA]
RewriteRule ^news.html$ /news/ [QSA,R]
RewriteRule ^news/page([0-9]*)?.html$ index.php?whatpage=news&newspage=$1 [L,QSA]
RewriteRule ^news/([-a-zA-Z0-9_]*)/?index.php$ /news/$1/ [QSA,R]
RewriteRule ^news/([-a-zA-Z0-9_]*)/?page([0-9]*)?.html$ index.php?whatpage=news&catnews=$1&newspage=$2 [L,QSA]
RewriteRule ^news/([-a-zA-Z0-9_]*)?.html$ index.php?whatpage=news&view=$1 [L,QSA]
RewriteRule ^news/([-a-zA-Z0-9_]*)/?$ index.php?whatpage=news&catnews=$1 [L,QSA]
RewriteRule ^news/([-a-zA-Z0-9_]*)/?([-a-zA-Z0-9_]*)?.html$ index.php?whatpage=news&catnews=$1&view=$2 [L,QSA]
# Guestbook
RewriteRule ^guestbook-start-([0-9]*).html?$ index.php?whatpage=guestbook&start=$1 [L,QSA]
RewriteRule ^guestbook-post-([0-9]*).html?$ index.php?whatpage=guestbook&id=$1 [L,QSA]
# General pages
RewriteRule ^([-a-zA-Z0-9_]*)?.html$ index.php?whatpage=$1 [L,QSA]
RewriteRule ^([-a-zA-Z0-9_]*)/?([-a-zA-Z0-9_]*)/?spage-([0-9]*)?.html$ index.php?catpage=$1&subcatpage=$2&spage=$3 [L,QSA]
RewriteRule ^([-a-zA-Z0-9_]*)/?([-a-zA-Z0-9_]*)?.html$ index.php?catpage=$1&whatpage=$2 [L,QSA]
RewriteRule ^([-a-zA-Z0-9_]*)/?$ index.php?catpage=$1 [L,QSA]
RewriteRule ^([-a-zA-Z0-9_]*)/?([-a-zA-Z0-9_]*)/?([-a-zA-Z0-9_]*)?.html$ index.php?catpage=$1&subcatpage=$2&whatpage=$3 [L,QSA]
RewriteRule ^([-a-zA-Z0-9_]*)/?([-a-zA-Z0-9_]*)/?$ index.php?catpage=$1&subcatpage=$2 [L,QSA]
# Serve existing files or directories directly
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
# Redirect all other requests to index.php
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]