1. Home
  2. Go Live Processes
  3. Building the .htaccess File

Building the .htaccess File

You will need your 301s before you can create a complete the .htaccess file.

Overview

This .htaccess configuration is optimized for our Plesk/Nginx environment, addressing common scenarios like domain redirection and URL rewriting, specifically for our needs.

Copy and paste the content below into your .htaccess file or download the template from the link above.

# .htaccess Configuration Template
# Version: 3.2
# Date: 2024-12-19
# Description: Simplified template for static HTML + WordPress blog on PLESK Apache + NGINX
# Instructions: Replace YOURDOMAIN.com with actual domain name

# Directory Options
Options +FollowSymLinks -MultiViews

# Enable Rewrite Engine
RewriteEngine On
RewriteBase /

# ---- HTTPS Enforcement ----
# Force HTTPS first (prevents redirect loops)
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# ---- Same Domain Redirection ----
# Redirect non-www to www (after HTTPS is established)
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^localhost [NC]
RewriteCond %{HTTP_HOST} ^(.+)$ [NC]
RewriteRule ^(.*)$ https://www.%1/$1 [L,R=301]

# ---- Cross-Domain Redirection (Optional) ----
# Uncomment and modify for domain migrations
# RewriteCond %{HTTP_HOST} ^(www\.)?OLDDOMAIN\.com$ [NC]
# RewriteRule ^(.*)$ https://www.YOURDOMAIN.com/$1 [L,R=301]

# ---- Index.html Redirection (Static HTML CMS) ----
# Redirect index.html to / (only for root level, not /blog/)
RewriteCond %{THE_REQUEST} /\index\.html [NC]
RewriteCond %{REQUEST_URI} !^/blog/ [NC]
RewriteRule ^index\.html$ / [L,R=301]

# ---- WordPress Blog Protection ----
# Ensure WordPress in /blog/ directory works properly
# (WordPress has its own .htaccess rules in the /blog/ directory)

# ---- 404 Error Handling ----
# Redirect all 404 errors to homepage (except blog URLs)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/blog/ [NC]
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^.*$ / [L,R=301]

# Fallback 404 document
ErrorDocument 404 https://www.YOURDOMAIN.com/

# ---- Specific Page Redirects ----
# Add your 301 redirects below:
# Redirect 301 /old-page.html https://www.YOURDOMAIN.com/new-page.html
# Redirect 301 /another-old-page.html https://www.YOURDOMAIN.com/

# ---- Installation Checklist ----
# 1. Replace all instances of YOURDOMAIN.com with actual domain
# 2. Replace "OLDDOMAIN" with actual old domain (if doing domain migration)
# 3. Add specific page redirects as needed
# 4. Ensure WordPress .htaccess exists in /blog/ directory
# 5. Test 404 handling by visiting non-existent page
# 6. Test www/non-www redirects
# 7. Test HTTP to HTTPS redirects
# 8. Test index.html redirects (root level only)
# 9. Test WordPress blog functionality in /blog/ 

Instructions for Creating and Editing.htaccess

  1. Creating the File: Use a text editor to create a file named .htaccess and place it in the directory you want to control.
  2. Editing the File: Use a text editor to add or modify directives.
  3. RewriteCond RewriteRule Directives: Leave ALL 3 Sets of if Unsure:
  4. Customize for Your Domain: Replace oldexample.com and newexample.com with your actual project URLs.

Backup: Always back up your .htaccess file before making changes.

Testing and Deployment

  1. Test your .htaccess file in a staging environment to ensure all redirects work as expected.
  2. Deploy final .htaccess file to your live environment.
  3. Use .htaccess tester tools to verify your rules.
  4. Monitor your website to ensure all redirects are functioning correctly.

made with love – .htaccess tester

Updated on September 3, 2024
Was this article helpful?

Related Articles