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.
Why Use .htaccess?
Section titled “Why Use .htaccess?”- 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.
Creating and Editing a .htaccess File
Section titled “Creating and Editing a .htaccess File”- Log in to your hosting account via FTP, SFTP, or File Manager.
- Navigate to the root directory of your website (often
public_html). - Create a new file named
.htaccess(if one doesn’t already exist). - Edit the file with your desired directives.
- Save changes and test your site.
⚠️ Important: Misconfigured rules can cause an “Internal Server Error.” Always back up your .htaccess file before editing.
Common .htaccess Tasks
Section titled “Common .htaccess Tasks”1. Redirect to HTTPS
Section titled “1. Redirect to HTTPS”RewriteEngine OnRewriteCond %{HTTPS} offRewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]2. Custom Error Pages
Section titled “2. Custom Error Pages”ErrorDocument 404 /errors/404.htmlErrorDocument 500 /errors/500.html3. Prevent Directory Listing
Section titled “3. Prevent Directory Listing”Options -Indexes4. Block Access to Specific Files
Section titled “4. Block Access to Specific Files”<FilesMatch "\.(htaccess|htpasswd)$"> Order allow,deny Deny from all</FilesMatch>5. Enable Caching
Section titled “5. Enable Caching”<IfModule mod_expires.c> ExpiresActive On ExpiresByType image/jpg "access plus 1 month" ExpiresByType text/css "access plus 1 week"</IfModule>Protecting the .htaccess File Itself
Section titled “Protecting the .htaccess File Itself”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.