| Server IP : 3.147.158.171 / Your IP : 216.73.216.88 Web Server : Apache/2.4.67 (Amazon Linux) OpenSSL/3.5.5 System : Linux ip-172-31-2-178.us-east-2.compute.internal 6.1.172-216.329.amzn2023.x86_64 #1 SMP PREEMPT_DYNAMIC Wed May 20 06:31:34 UTC 2026 x86_64 User : ec2-user ( 1000) PHP Version : 8.4.21 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /tsai/repo/builder/backend/scripts/ |
Upload File : |
#!/usr/bin/env python3
"""
Write Htaccess Files
Generates .htaccess files for Angular i18n apps (creator) with language-based routing.
Used by the builder config for Angular app builds.
Usage:
python write_htaccess.py <output_path> [rewrite_path]
Example:
python write_htaccess.py /path/to/chatbot/dist/production/creator/browser ""
"""
import os
import sys
def write_main_htaccess(output_path: str, rewrite_path: str = "") -> None:
"""Write the main .htaccess file with language detection routing."""
lines = [
'RewriteEngine on',
'RewriteBase /',
'RewriteRule ^.well-known/ - [L]',
'RewriteRule ^../index\\.html$ - [L]',
'',
'RewriteCond %{REQUEST_FILENAME} !-f',
'RewriteCond %{REQUEST_FILENAME} !-d',
'RewriteRule (..) $1/index.html [L]',
'',
'RewriteCond %{HTTP:Accept-Language} es [NC]',
'RewriteRule ^$ /es/ [R]',
'',
'RewriteCond %{HTTP:Accept-Language} ^fr [NC]',
'RewriteRule ^$ /fr/ [R]',
'',
'RewriteCond %{HTTP:Accept-Language} !^es [NC]',
'RewriteCond %{HTTP:Accept-Language} !^fr [NC]',
f'RewriteRule ^$ {rewrite_path}/en-US/ [R]'
]
htaccess_path = os.path.join(output_path, '.htaccess')
with open(htaccess_path, 'w') as f:
for line in lines:
f.write(line + '\n')
print(f"Main .htaccess written: {htaccess_path}")
def write_subdir_htaccess(output_path: str) -> None:
"""Write .htaccess file for language subdirectory."""
lines = [
'RewriteEngine on',
'# If an existing asset or directory is requested go to it as it is',
'RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]',
'RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d',
'RewriteRule ^ - [L]',
'RewriteRule ^ index.html',
"# If the requested resource doesn't exist, use index.html"
]
with open(output_path, 'w') as f:
for line in lines:
f.write(line + '\n')
print(f"Subdir .htaccess written: {output_path}")
def write_htaccess_files(output_path: str, rewrite_path: str = "") -> None:
"""Write all .htaccess files for an i18n Angular app."""
# Write main .htaccess
write_main_htaccess(output_path, rewrite_path)
# Write subdirectory .htaccess files for each locale
for locale in ['en-US', 'es', 'fr']:
subdir_path = os.path.join(output_path, locale, '.htaccess')
# Only write if the locale directory exists
locale_dir = os.path.join(output_path, locale)
if os.path.exists(locale_dir):
write_subdir_htaccess(subdir_path)
else:
print(f"Skipping {locale} (directory not found)")
def main():
if len(sys.argv) < 2:
print("Usage: python write_htaccess.py <output_path> [rewrite_path]")
sys.exit(1)
output_path = sys.argv[1]
rewrite_path = sys.argv[2] if len(sys.argv) > 2 else ""
if not os.path.exists(output_path):
print(f"Error: Directory not found: {output_path}")
sys.exit(1)
write_htaccess_files(output_path, rewrite_path)
print("All .htaccess files written successfully")
if __name__ == "__main__":
main()