.htaccess 813 B

1234567891011121314151617181920212223242526272829
  1. RewriteEngine On
  2. <IfModule mod_rewrite.c>
  3. # 1. Redirect HTTP to HTTPS
  4. RewriteCond %{HTTPS} off
  5. RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  6. # 2. Trim URL to the first element
  7. RewriteCond %{REQUEST_URI} ^/([^/]+)(/.*|\.php.*)$
  8. RewriteCond %{REQUEST_FILENAME} !-d
  9. RewriteCond %{REQUEST_FILENAME} !-f
  10. RewriteRule .* /%1 [R=301,L]
  11. # 3. Handle directories (add trailing slash)
  12. RewriteCond %{REQUEST_FILENAME} -d
  13. RewriteRule ^([^/]+)$ /$1/ [R=301,L]
  14. # 4. Handle PHP files (remove .php and serve if file exists)
  15. RewriteCond %{REQUEST_FILENAME}.php -f
  16. RewriteRule ^([^/]+)$ /$1.php [NC,L]
  17. # 5. Deny access to
  18. RewriteRule ^\.git/ - [R=403,L]
  19. </IfModule>
  20. # Custom error pages
  21. ErrorDocument 404 /error.php