RewriteEngine On
    
RewriteRule ^/?sitemap.xml/?$ sitemap.php [L]
    
RewriteCond %{REQUEST_URI} !^/admin/
RewriteRule ^([^/]+)/assets/(.*)$ /public/view/theme/$1/assets/$2 [L]

# Rule to remove trailing slash if there's a query string
RewriteCond %{QUERY_STRING} !=""
RewriteCond %{REQUEST_URI} /(.+)/$
RewriteRule ^ %1?%{QUERY_STRING} [L,R=301]

# Rule to add trailing slash if there's no query string
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{QUERY_STRING} =""
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]

# untuk sitemap
RewriteRule ^/?sitemap.xml/?$ sitemap.php [L]

    # SIMPEN
    RewriteRule ^/?simpen/?$ simpen.php [L]
    RewriteRule ^/?simpen/([^/]+)/?$ simpen.php?menu=$1 [L]
    RewriteRule ^/?simpen/([^/]+)/page/([0-9]+)/?$ simpen.php?menu=$1&page=$2 [L]
    RewriteRule ^/?simpen/([^/]+)/([^/]+)/?$ simpen.php?menu=$1&link=$2 [L]
    RewriteRule ^/?simpen/([^/]+)/([^/]+)/([^/]+)/?$ simpen.php?menu=$1&link=$2&aksi=$3 [L]
    RewriteRule ^/?simpen/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ simpen.php?menu=$1&link=$2&aksi=$3&no=$4 [L]
    
    # PPDB
    RewriteRule ^/?spmb/?$ ppdb.php [L]
    RewriteRule ^/?spmb/([^/]+)/?$ ppdb.php?menu=$1 [L]
    RewriteRule ^/?spmb/([^/]+)/page/([0-9]+)/?$ ppdb.php?menu=$1&page=$2 [L]
    RewriteRule ^/?spmb/([^/]+)/([^/]+)/?$ ppdb.php?menu=$1&link=$2 [L]
    RewriteRule ^/?spmb/([^/]+)/([^/]+)/([^/]+)/?$ ppdb.php?menu=$1&link=$2&aksi=$3 [L]
    RewriteRule ^/?spmb/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ ppdb.php?menu=$1&link=$2&aksi=$3&no=$4 [L]
    
    #PTSP
    RewriteRule ^/?ptsp/?$ ptsp.php [L]
    RewriteRule ^/?ptsp/([^/]+)/?$ ptsp.php?menu=$1 [L]
    RewriteRule ^/?ptsp/([^/]+)/page/([0-9]+)/?$ ptsp.php?menu=$1&page=$2 [L]
    RewriteRule ^/?ptsp/([^/]+)/([^/]+)/?$ ptsp.php?menu=$1&link=$2 [L]
    RewriteRule ^/?ptsp/([^/]+)/([^/]+)/([^/]+)/?$ ptsp.php?menu=$1&link=$2&aksi=$3 [L]
    RewriteRule ^/?ptsp/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ ptsp.php?menu=$1&link=$2&aksi=$3&no=$4 [L]


# Admin routes
RewriteRule ^/?adm/?$ admin.php [L]
RewriteRule ^/?adm/([^/]+)/?$ admin.php?menu=$1 [QSA,L]
RewriteRule ^/?adm/([^/]+)$ admin.php?menu=$1 [L]
RewriteRule ^/?adm/([^/]+)/([^/]+)/?$ admin.php?menu=$1&act=$2 [L]
RewriteRule ^/?adm/([^/]+)/([^/]+)/([^/]+)/$ admin.php?menu=$1&act=$2&no=$3 [L]

# General routes
RewriteRule ^/?([A-Za-z0-9_-]+)/?$ index.php?menu=$1 [L]
RewriteRule ^/?([A-Za-z0-9_-]+)/page/([0-9]+)/?$ index.php?menu=$1&page=$2 [L]
RewriteRule ^/?([A-Za-z0-9_-]+)/([^/]+)/?$ index.php?menu=$1&link=$2 [L]
RewriteRule ^/?([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/?$ index.php?menu=$1&link=$2 [L]
RewriteRule ^/?([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/page/([0-9]+)/?$ index.php?menu=$1&link=$2&page=$3 [L]
RewriteRule ^/?([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/([0-9]+)/([A-Za-z0-9_-]+)/?$ index.php?menu=$1&link=$2&no=$3&act=$4 [L]
RewriteRule ^/?([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/([0-9]+)/([A-Za-z0-9_-]+)/page/([0-9]+)/?$ index.php?menu=$1&link=$2&no=$3&act=$4&page=$5 [L]
RewriteRule ^/?([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/([0-9]+)/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/?$ index.php?menu=$1&link=$2&no=$3&act=$4&orderby=$5&by=$6 [L]
RewriteRule ^/?([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/([0-9]+)/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/page/([0-9]+)/?$ index.php?menu=$1&link=$2&no=$3&act=$4&orderby=$5&by=$6&page=$7 [L]

# Deny access to .htaccess
<Files .htaccess>
    Order allow,deny
    Deny from all
</Files>

# Disable directory browsing
Options -Indexes

# Enable CORS
<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
    Header set Access-Control-Allow-Headers "Content-Type"
</IfModule>

# Set default expiration
ExpiresDefault "access plus 1 month"

# Set images expiration
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 month"

# Set page scripts expiration
ExpiresByType text/css "access plus 1 week"
ExpiresByType text/x-javascript "access plus 1 month"

<IfModule mod_deflate.c>
  # Force compression for mangled headers
  <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>

  # Map file types to encoding
  <IfModule mod_mime.c>
    AddEncoding gzip svgz
  </IfModule>

  # Compress output for specific media types
  <IfModule mod_filter.c>
    AddOutputFilterByType DEFLATE "application/atom+xml" \
      "application/javascript" \
      "application/json" \
      "application/ld+json" \
      "application/manifest+json" \
      "application/rdf+xml" \
      "application/rss+xml" \
      "application/schema+json" \
      "application/vnd.geo+json" \
      "application/vnd.ms-fontobject" \
      "application/x-font-ttf" \
      "application/x-web-app-manifest+json" \
      "application/xhtml+xml" \
      "application/xml" \
      "font/opentype" \
      "image/svg+xml" \
      "image/x-icon" \
      "text/cache-manifest" \
      "text/css" \
      "text/html" \
      "text/javascript" \
      "text/plain" \
      "text/vtt" \
      "text/x-component" \
      "text/xml"
  </IfModule>
</IfModule>

<IfModule mod_headers.c>
  # Set font file access control
  <FilesMatch "\.(eot|ttf|otf|woff2|svg)$">
    Header set Access-Control-Allow-Origin "*"
  </FilesMatch>

  # Uncomment to enable HSTS
  # Header set Strict-Transport-Security "max-age=31536000" env=HTTPS
</IfModule>
