# =============================================================================
#  BMTC Qatar — Apache configuration
#  Canonical form: https://bmtcdoha.com  (HTTPS, no www, no trailing slash)
#
#  NOTE: this file assumes the site is served from the /BMTCDOHA/ subfolder
#  during local development. When deploying to the domain root, replace
#  every "/BMTCDOHA/" below with "/" and set basePath to "" in
#  _tools/business-info.json, then re-run: node _tools/sync.js
# =============================================================================

Options -Indexes
DirectoryIndex index.html

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /BMTCDOHA/

    # ---------------------------------------------------------------------
    # 1. Canonical host + protocol (skipped on local development hosts)
    # ---------------------------------------------------------------------
    RewriteCond %{HTTP_HOST} !^(localhost|127\.0\.0\.1)(:\d+)?$ [NC]

    # http -> https
    RewriteCond %{HTTPS} !=on
    RewriteRule ^(.*)$ https://bmtcdoha.com%{REQUEST_URI} [R=301,L]

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

    # ---------------------------------------------------------------------
    # 2. Strip trailing slashes (canonical URLs never end in /)
    #    Directory requests are left alone.
    # ---------------------------------------------------------------------
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)/$ /BMTCDOHA/$1 [R=301,L]

    # ---------------------------------------------------------------------
    # 3. Legacy .html extensions -> clean canonical URLs
    #    Only redirect when the extension was actually typed.
    # ---------------------------------------------------------------------
    RewriteCond %{THE_REQUEST} \s/+BMTCDOHA/index\.html[\s\?] [NC]
    RewriteRule ^index\.html$ /BMTCDOHA/ [R=301,L]

    RewriteCond %{THE_REQUEST} \s/+BMTCDOHA/(about|contact|services|locations|blog|gallery)\.html[\s\?] [NC]
    RewriteRule ^(about|contact|services|locations|blog|gallery)\.html$ /BMTCDOHA/$1 [R=301,L]

    # Clean aliases used by the IA
    RewriteRule ^about-us/?$ about.html [L,QSA]
    RewriteRule ^contact-us/?$ contact.html [L,QSA]

    # ---------------------------------------------------------------------
    # 4. Flat service URLs -> nested category URLs (301)
    #    /services/commercial-interior-fit-out
    #      -> /services/interior-fit-out/commercial-interior-fit-out
    # ---------------------------------------------------------------------
    RewriteCond %{THE_REQUEST} \s/+BMTCDOHA/services/[\s\?] [NC]
    RewriteRule ^services/(commercial-interior-fit-out|corporate-office-fit-out|retail-shop-fit-out|hospitality-restaurant-fit-out|interior-space-planning-3d-design|custom-joinery-woodwork|mep-services|false-ceiling-partitions-flooring|glass-aluminum-works|municipality-civil-defence-compliance)/?$ /BMTCDOHA/services/interior-fit-out/$1 [R=301,L]

    RewriteCond %{THE_REQUEST} \s/+BMTCDOHA/services/[\s\?] [NC]
    RewriteRule ^services/(3d-signage|led-neon-signage|acrylic-signage|indoor-outdoor-signage|corporate-signage|wayfinding-safety-signage|vehicle-window-graphics|signage-design-installation)/?$ /BMTCDOHA/services/signage/$1 [R=301,L]

    RewriteCond %{THE_REQUEST} \s/+BMTCDOHA/services/[\s\?] [NC]
    RewriteRule ^services/(exhibition-stand-design|exhibition-stand-construction|custom-exhibition-stands|exhibition-booths|kiosks-display-stands|retail-displays|event-promotional-displays)/?$ /BMTCDOHA/services/exhibition/$1 [R=301,L]

    # Root-level service files, e.g. /BMTCDOHA/3d-signage.html
    RewriteCond %{THE_REQUEST} \s/+BMTCDOHA/(commercial-interior-fit-out|corporate-office-fit-out|retail-shop-fit-out|hospitality-restaurant-fit-out|interior-space-planning-3d-design|custom-joinery-woodwork|mep-services|false-ceiling-partitions-flooring|glass-aluminum-works|municipality-civil-defence-compliance)(\.html)?[\s\?] [NC]
    RewriteRule ^(commercial-interior-fit-out|corporate-office-fit-out|retail-shop-fit-out|hospitality-restaurant-fit-out|interior-space-planning-3d-design|custom-joinery-woodwork|mep-services|false-ceiling-partitions-flooring|glass-aluminum-works|municipality-civil-defence-compliance)(\.html)?/?$ /BMTCDOHA/services/interior-fit-out/$1 [R=301,L]

    RewriteCond %{THE_REQUEST} \s/+BMTCDOHA/(3d-signage|led-neon-signage|acrylic-signage|indoor-outdoor-signage|corporate-signage|wayfinding-safety-signage|vehicle-window-graphics|signage-design-installation|signage)(\.html)?[\s\?] [NC]
    RewriteRule ^(3d-signage|led-neon-signage|acrylic-signage|indoor-outdoor-signage|corporate-signage|wayfinding-safety-signage|vehicle-window-graphics|signage-design-installation)(\.html)?/?$ /BMTCDOHA/services/signage/$1 [R=301,L]
    RewriteCond %{THE_REQUEST} \s/+BMTCDOHA/signage(\.html)?[\s\?] [NC]
    RewriteRule ^signage(\.html)?/?$ /BMTCDOHA/services/signage [R=301,L]

    RewriteCond %{THE_REQUEST} \s/+BMTCDOHA/(exhibition-stand-design|exhibition-stand-construction|custom-exhibition-stands|exhibition-booths|kiosks-display-stands|retail-displays|event-promotional-displays|exhibition)(\.html)?[\s\?] [NC]
    RewriteRule ^(exhibition-stand-design|exhibition-stand-construction|custom-exhibition-stands|exhibition-booths|kiosks-display-stands|retail-displays|event-promotional-displays)(\.html)?/?$ /BMTCDOHA/services/exhibition/$1 [R=301,L]
    RewriteCond %{THE_REQUEST} \s/+BMTCDOHA/exhibition(\.html)?[\s\?] [NC]
    RewriteRule ^exhibition(\.html)?/?$ /BMTCDOHA/services/exhibition [R=301,L]

    # ---------------------------------------------------------------------
    # 5. Location URLs -> /locations/<slug>
    # ---------------------------------------------------------------------
    RewriteCond %{THE_REQUEST} \s/+BMTCDOHA/(doha|west-bay|lusail|the-pearl|al-rayyan|al-wakrah|industrial-area)(\.html)?[\s\?] [NC]
    RewriteRule ^(doha|west-bay|lusail|the-pearl|al-rayyan|al-wakrah|industrial-area)(\.html)?/?$ /BMTCDOHA/locations/$1 [R=301,L]

    # ---------------------------------------------------------------------
    # 6. Blog article URLs -> /blog/<slug>
    # ---------------------------------------------------------------------
    RewriteCond %{THE_REQUEST} \s/+BMTCDOHA/(qatar-office-fit-out-cost-guide-2026|fast-tracking-baladiya-civil-defence-approvals-qatar|exhibition-stand-design-trends-decc-qncc-doha)(\.html)?[\s\?] [NC]
    RewriteRule ^(qatar-office-fit-out-cost-guide-2026|fast-tracking-baladiya-civil-defence-approvals-qatar|exhibition-stand-design-trends-decc-qncc-doha)(\.html)?/?$ /BMTCDOHA/blog/$1 [R=301,L]

    # ---------------------------------------------------------------------
    # 7. Removed stub pages -> their real destinations (301)
    # ---------------------------------------------------------------------
    RewriteRule ^coming-soon-municipality-civil-defence-compliance(?:\.html)?/?$ /BMTCDOHA/services/interior-fit-out/municipality-civil-defence-compliance [R=301,L]
    RewriteRule ^coming-soon-acrylic-signage(?:\.html)?/?$ /BMTCDOHA/services/signage/acrylic-signage [R=301,L]
    RewriteRule ^coming-soon-wayfinding-safety-signage(?:\.html)?/?$ /BMTCDOHA/services/signage/wayfinding-safety-signage [R=301,L]
    RewriteRule ^coming-soon-vehicle-window-graphics(?:\.html)?/?$ /BMTCDOHA/services/signage/vehicle-window-graphics [R=301,L]
    RewriteRule ^coming-soon-exhibition-stand-design(?:\.html)?/?$ /BMTCDOHA/services/exhibition/exhibition-stand-design [R=301,L]
    RewriteRule ^coming-soon-exhibition-stand-construction(?:\.html)?/?$ /BMTCDOHA/services/exhibition/exhibition-stand-construction [R=301,L]
    RewriteRule ^coming-soon-custom-exhibition-stands(?:\.html)?/?$ /BMTCDOHA/services/exhibition/custom-exhibition-stands [R=301,L]
    RewriteRule ^coming-soon-exhibition-booths(?:\.html)?/?$ /BMTCDOHA/services/exhibition/exhibition-booths [R=301,L]
    RewriteRule ^coming-soon-kiosks-display-stands(?:\.html)?/?$ /BMTCDOHA/services/exhibition/kiosks-display-stands [R=301,L]
    RewriteRule ^coming-soon-false-ceiling-partitions-flooring(?:\.html)?/?$ /BMTCDOHA/services/interior-fit-out/false-ceiling-partitions-flooring [R=301,L]
    RewriteRule ^coming-soon-glass-aluminum-works(?:\.html)?/?$ /BMTCDOHA/services/interior-fit-out/glass-aluminum-works [R=301,L]
    RewriteRule ^coming-soon-al-rayyan(?:\.html)?/?$ /BMTCDOHA/locations/al-rayyan [R=301,L]
    RewriteRule ^coming-soon-al-wakrah(?:\.html)?/?$ /BMTCDOHA/locations/al-wakrah [R=301,L]
    RewriteRule ^coming-soon-industrial-area(?:\.html)?/?$ /BMTCDOHA/locations/industrial-area [R=301,L]
    RewriteRule ^coming-soon-blog(?:\.html)?/?$ /BMTCDOHA/blog [R=301,L]
    RewriteRule ^coming-soon(?:\.html)?/?$ /BMTCDOHA/ [R=301,L]

    # Leftover HTML-template pages that were never part of this site.
    RewriteRule ^(news|news-grid|news-details|room|room-details|service|service-details|team|team-details|testimonial|facilitie)(?:\.html)?/?$ /BMTCDOHA/ [R=301,L]

    # ---------------------------------------------------------------------
    # 8. Internal rewrites (canonical URL -> real file). No redirect.
    # ---------------------------------------------------------------------
    RewriteRule ^services/?$ services.html [L,QSA]
    RewriteRule ^locations/?$ locations.html [L,QSA]
    RewriteRule ^blog/?$ blog.html [L,QSA]

    # Category hubs
    RewriteRule ^services/(interior-fit-out|signage|exhibition)/?$ $1.html [L,QSA]

    # Service leaves: /services/<category>/<leaf> -> <leaf>.html
    RewriteCond %{DOCUMENT_ROOT}/BMTCDOHA/$2.html -f
    RewriteRule ^services/(interior-fit-out|signage|exhibition)/([a-z0-9-]+)/?$ $2.html [L,QSA]

    # Location pages
    RewriteCond %{DOCUMENT_ROOT}/BMTCDOHA/$1.html -f
    RewriteRule ^locations/([a-z0-9-]+)/?$ $1.html [L,QSA]

    # Blog articles
    RewriteCond %{DOCUMENT_ROOT}/BMTCDOHA/$1.html -f
    RewriteRule ^blog/([a-z0-9-]+)/?$ $1.html [L,QSA]

    # Any remaining extensionless page
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME}\.html -f
    RewriteRule ^(.*)$ $1.html [L,QSA]

    # Collapse accidental duplicate slashes
    RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
    RewriteRule ^ %1/%2 [R=301,L]
</IfModule>

# =============================================================================
#  Error handling
# =============================================================================
ErrorDocument 404 /BMTCDOHA/404.html

# =============================================================================
#  Security headers
# =============================================================================
<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=(), interest-cohort=()"
    Header always set Cross-Origin-Opener-Policy "same-origin"

    # HSTS — only meaningful over HTTPS. Uncomment once the site is live on TLS
    # and you have confirmed every subdomain is HTTPS-capable.
    # Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"

    # Do not advertise the server implementation.
    Header unset X-Powered-By
    Header unset Server
</IfModule>

# =============================================================================
#  Compression
# =============================================================================
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript
    AddOutputFilterByType DEFLATE application/javascript application/x-javascript
    AddOutputFilterByType DEFLATE application/json application/xml
    AddOutputFilterByType DEFLATE application/rss+xml
    AddOutputFilterByType DEFLATE image/svg+xml

    # Correct handling for legacy browsers
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4\.0[678] no-gzip
    BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</IfModule>

<IfModule mod_brotli.c>
    AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/css text/javascript
    AddOutputFilterByType BROTLI_COMPRESS application/javascript application/json
    AddOutputFilterByType BROTLI_COMPRESS image/svg+xml
</IfModule>

# =============================================================================
#  Caching
# =============================================================================
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault                              "access plus 1 month"

    # HTML must revalidate so content updates appear immediately.
    ExpiresByType text/html                     "access plus 0 seconds"

    # Fingerprinted / rarely changing assets
    ExpiresByType text/css                      "access plus 1 year"
    ExpiresByType application/javascript        "access plus 1 year"
    ExpiresByType text/javascript               "access plus 1 year"

    ExpiresByType image/jpeg                    "access plus 1 year"
    ExpiresByType image/png                     "access plus 1 year"
    ExpiresByType image/webp                    "access plus 1 year"
    ExpiresByType image/avif                    "access plus 1 year"
    ExpiresByType image/svg+xml                 "access plus 1 year"
    ExpiresByType image/x-icon                  "access plus 1 year"

    ExpiresByType font/woff                     "access plus 1 year"
    ExpiresByType font/woff2                    "access plus 1 year"
    ExpiresByType application/font-woff         "access plus 1 year"
    ExpiresByType font/ttf                      "access plus 1 year"

    ExpiresByType application/xml               "access plus 1 hour"
    ExpiresByType text/plain                    "access plus 1 hour"
</IfModule>

<IfModule mod_headers.c>
    <FilesMatch "\.(css|js|jpg|jpeg|png|gif|webp|avif|svg|ico|woff|woff2|ttf|eot)$">
        Header set Cache-Control "public, max-age=31536000, immutable"
    </FilesMatch>
    <FilesMatch "\.(html|php)$">
        Header set Cache-Control "public, max-age=0, must-revalidate"
    </FilesMatch>
</IfModule>

# =============================================================================
#  Protect internal tooling, backups and captured leads
# =============================================================================
<IfModule mod_rewrite.c>
    RewriteRule ^(_tools|_leads|_backup_pre_audit|scratch)/ - [F,L]
</IfModule>

<FilesMatch "\.(json|md|log|scss|map)$">
    <IfModule mod_authz_core.c>
        Require all denied
    </IfModule>
    <IfModule !mod_authz_core.c>
        Order allow,deny
        Deny from all
    </IfModule>
</FilesMatch>

# Block direct access to the lead log directory contents
RedirectMatch 404 ^/BMTCDOHA/_leads/

# =============================================================================
#  MIME types
# =============================================================================
<IfModule mod_mime.c>
    AddType image/webp    .webp
    AddType image/avif    .avif
    AddType image/svg+xml .svg
    AddType font/woff2    .woff2
    AddType application/manifest+json .webmanifest
</IfModule>

# =============================================================================
#  Character set
# =============================================================================
AddDefaultCharset UTF-8
