<IfModule mod_rewrite.c>
    RewriteEngine On

    # === BLOCK PROBES + LOG THEM (unified patterns) ===
    # These patterns will be blocked with 403 and logged
    RewriteCond %{REQUEST_URI} (\.env|wp-config\.php|\.git/|node_modules/|vite/client|tests/|mail/|kyc/|backup/|tmp/|database/|storage/|config/|api/shared/|\.vscode/|v[0-9]+/) [NC,OR]
    RewriteCond %{REQUEST_URI} \.(bak|save|old|dist|example|local|prod|staging|test|template|aws|stripe) [NC]
    RewriteRule .* - [E=probe_blocked:1,F,L,NC]
</IfModule>

<IFModule mod_headers.c>
    Header edit Set-Cookie ^(CraftSessionId.*)$ $1;HttpOnly;Secure;SameSite=None
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Password protect staging server
    SetEnvIf Host airgroup.preview.dev passreq
    AuthType Basic
    AuthName "Password Required"
    AuthUserFile /var/www/airgroup.com/current/.htpasswd
    Require valid-user
    Order allow,deny
    Allow from all
    Deny from env=passreq
    Satisfy any

</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Rewrite www.example.com → example.com
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Redirect existing tracking links to our new url pattern
    RewriteCond %{QUERY_STRING} ^action=hawb_coord&key=([0-9]+)$ [NC]
    RewriteRule ^(main|standalone).php$ /track?key=%1 [NC,L,R=301]

    # Redirect existing id and shiptype requests to our new url pattern
    RewriteCond %{QUERY_STRING} ^action=hawb_coord&id=([0-9]+)&shiptype=([0-9])$ [NC]
    RewriteRule ^(main|standalone).php$ /track?key=0001%2%1 [NC,L,R=301]

    # Redirect existing hawb requests to our new hawb lookup url
    RewriteCond %{QUERY_STRING} ^action=hawb_coord&hawb=([0-9]+)$ [NC]
    RewriteRule ^(main|standalone).php$ /track?hawb=%1 [NC,L,R=301]

    # Redirect existing hawb requests to our new hawb lookup url (alternate)
    RewriteCond %{QUERY_STRING} ^action=hawb_track&hawb=([0-9]+)$ [NC]
    RewriteRule ^(main|standalone).php$ /track?hawb=%1 [NC,L,R=301]

    # Redirect new id and shiptype requests converted to tracking link
    RewriteCond %{QUERY_STRING} ^id=([0-9]+)&shiptype=([0-9])$ [NC]
    RewriteRule ^track$ /track?key=0001%2%1 [NC,L,R=301]

    # Redirect existing links to new payment portal site
    RewriteCond %{QUERY_STRING} ^action=payment [NC]
    RewriteRule ^(main|standalone).php$ https://payments.airgroup.com? [NC,L,R=301]

    # Redirect existing links to new tarriff quote site
    RewriteCond %{QUERY_STRING} ^action=signon&type=cust_quote [NC]
    RewriteRule ^(main|standalone).php$ http://globalship.airgroup.com/tariffquote? [NC,L,R=301]

    # Redirect existing links to new magaya
    RewriteCond %{QUERY_STRING} ^action=signon&type=wms [NC]
    RewriteRule ^(main|standalone).php$ http://globalship.airgroup.com/wms? [NC,L,R=301]
    RewriteRule ^terms$ https://resources.radiantdelivers.com/airgroup-terms-and-conditions [NC,L,R=301]

</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Send would-be 404 requests to Craft
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
    RewriteRule (.+) index.php?p=$1 [QSA,L]
</IfModule>
