# Using .htaccess files

> Create and edit .htaccess files to set redirects, custom error pages, HTTPS, caching and access rules for your site.

Source: https://helpcenter.ruachost.com/developers/web-servers/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?

- 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

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.

## Common .htaccess Tasks

### 1. Redirect to HTTPS

```
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
```

### 2. Custom Error Pages

```
ErrorDocument 404 /errors/404.html
ErrorDocument 500 /errors/500.html
```

### 3. Prevent Directory Listing

```
Options -Indexes
```

### 4. Block Access to Specific Files

```

    Order allow,deny
    Deny from all

```

### 5. Enable Caching

```

    ExpiresActive On
    ExpiresByType image/jpg "access plus 1 month"
    ExpiresByType text/css "access plus 1 week"

```

## Protecting the `.htaccess` File Itself

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

```

    Order allow,deny
    Deny from all

```

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

-

> **Important Notes:**
- `.htaccess` applies to the directory it resides in and all subdirectories.
- Ruachost servers use `AllowOverride All` to maximize flexibility.
- Syntax errors can break your site — test changes carefully.
- For advanced tasks, consult the Apache .htaccess documentation.
