Skip to content

Using .htaccess files

The .htaccess file is a plain‑text configuration file used by the Apache web server to override global server settings for specific directories. On ruachost.com, you can use .htaccess files to customize how your website behaves without needing root access.

  • Redirect visitors to different URLs.
  • Control directory listings.
  • Specify custom error pages.
  • Enforce HTTPS connections.
  • Improve performance with caching and compression.
  • Restrict access to certain files or directories.
  1. Log in to your hosting account via FTP, SFTP, or File Manager.
  2. Navigate to the root directory of your website (often public_html).
  3. Create a new file named .htaccess (if one doesn’t already exist).
  4. Edit the file with your desired directives.
  5. Save changes and test your site.

⚠️ Important: Misconfigured rules can cause an “Internal Server Error.” Always back up your .htaccess file before editing.

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
ErrorDocument 404 /errors/404.html
ErrorDocument 500 /errors/500.html
Options -Indexes
<FilesMatch "\.(htaccess|htpasswd)$">
Order allow,deny
Deny from all
</FilesMatch>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType text/css "access plus 1 week"
</IfModule>

By default, anyone can attempt to view .htaccess files. To prevent this:

<FilesMatch "^\.htaccess">
Order allow,deny
Deny from all
</FilesMatch>

Visitors who try to access the file directly will see a 403 Forbidden error.