# Adding or removing the www prefix in domain URLs

> Redirect visitors so your site always uses or never uses the www prefix, using rules in your .htaccess file.

Source: https://helpcenter.ruachost.com/developers/web-servers/adding-or-removing-the-www-prefix-in-domain-urls/

On **ruachost.com**, you can configure your website to either **always include** or **always remove** the `www` prefix in URLs. This ensures consistency, improves SEO, and prevents duplicate content issues.

## Why Control the `www` Prefix?

- **SEO benefits**: Search engines may treat `www.example.com` and `example.com` as separate sites.
- **Brand consistency**: Visitors always see the same format.
- **Avoid duplicate content penalties**.
- **Better user experience**: Redirects visitors to the preferred version automatically.

## Adding the `www` Prefix

To force all visitors to use `www.example.com`:

1. Open or create the `.htaccess` file in your site’s root directory (`public_html`).
2. Add the following rewrite rules:

   ```
   ## Add www to any URLs that do not have them:
   RewriteEngine On
   RewriteCond %{HTTP_HOST} !^www\.
   RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
   ```

3. Save the file.
4. Test by visiting `http://example.com` — it should redirect to `http://www.example.com`.

## Removing the `www` Prefix

To force all visitors to use `example.com` without `www`:

1. Open or create the `.htaccess` file in your site’s root directory.
2. Add the following rewrite rules:

   ```
   ## Remove www from any URLs that have them:
   RewriteEngine On
   RewriteCond %{HTTP_HOST} ^www\.
   RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
   ```

3. Replace `example.com` with your actual domain name.
4. Save the file.
5. Test by visiting `http://www.example.com` — it should redirect to `http://example.com`.

## Important Notes

- Use **301 redirects** for SEO‑friendly permanent redirection.
- Only configure one option (add or remove) to avoid redirect loops.
- If your site uses HTTPS, replace `http://` with `https://` in the rules.
- Always back up your `.htaccess` file before editing.
