Web Server Configuration: Apache and .htaccess

Format: Mastery Session, teacher-led, about 60 minutes plus a 10-minute lab.
Audience: Customer Success specialists working with managed hosting (mostly WordPress).

Learning goals. By the end, a specialist should be able to:

1. Where we stand: Apache-compatible hosting (5 min)

We can doSysadmins onlyCustomer / developer
Read and troubleshoot .htaccess, check logs, change PHP options in hPanel, run read-only checks over SSH Main server config (httpd.conf, virtual hosts), AllowOverride, ServerTokens, timeouts, service restarts Writing custom rewrite logic for their project, plugin updates, code fixes
Note for the teacher. Directives such as AllowOverride, ServerTokens and Timeout only work in the main server config. Inside .htaccess they are ignored or cause a 500. If a customer pastes them from a tutorial, that is often the cause.

2. Anatomy of a WordPress .htaccess (10 min)

The default block that WordPress writes when permalinks are saved:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Flags to know

FlagMeaning
[L]Last: stop processing further rules in this pass.
[R=301] / [R=302]External redirect, permanent or temporary. Test with 302 first: browsers cache 301s.
[NC]No case: match is case-insensitive.
[QSA]Keep the query string (?utm=…) when the rule adds its own.

Things that do not belong there

3. Changing .htaccess safely (5 min)

  1. Back up first: copy it to .htaccess.bak-YYYYMMDD in File Manager or over SSH.
  2. Change one thing at a time.
  3. Test immediately. There is no configtest for .htaccess: the next request is the test. Check with curl -I https://domain.tld (status and headers) or curl -IL to follow the whole redirect chain.
  4. Read the error log if the result is a 500.
  5. Roll back right away if it breaks. Nothing needs a restart: .htaccess is read on every request.
Isolation trick. Rename .htaccess to .htaccess_old. If the error disappears, the cause is inside the file. For WordPress, regenerate a clean one with Settings → Permalinks → Save Changes, then add custom rules back one by one.

4. Troubleshooting by error (20 min)

500 Internal Server Error

403 Forbidden

Redirect loops (ERR_TOO_MANY_REDIRECTS) and 301/302

HTTP to HTTPS, and non-www to www:

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

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Usual causes of a loop:

Sources: non-www redirect guide, 302 error guide

413 Request Entity Too Large

Source: 413 guide

504 Gateway Timeout

Source: 504 guide

Cloudflare Error 520

Source: Error 520 guide

5. WordPress hardening with .htaccess (10 min)

# Disable directory listing
Options -Indexes

# Protect wp-config.php
<Files wp-config.php>
  Require all denied
</Files>

# Block XML-RPC (breaks Jetpack and the WordPress mobile app)
<Files xmlrpc.php>
  Require all denied
</Files>

# Basic security headers
<IfModule mod_headers.c>
  Header always set X-Content-Type-Options "nosniff"
  Header always set X-Frame-Options "SAMEORIGIN"
  Header always set Referrer-Policy "strict-origin-when-cross-origin"
</IfModule>

Block PHP execution in uploads with a separate file at wp-content/uploads/.htaccess:

<FilesMatch "\.php$">
  Require all denied
</FilesMatch>
Hiding the server version (ServerTokens) and choosing TLS versions are server-level settings. They do not work in .htaccess and are handled on our side.

6. Where to look first (5 min)

Lab (10 minutes)

Give each specialist one of the broken .htaccess files below. Ask them to find the cause with curl -I, the error log and the isolation trick, and to explain the fix as they would to a customer.

Case A: the redirect never happens

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Redirect 301 /old-page/ https://example.com/new-page/
Answer

The WordPress block already rewrites the request to index.php, so the custom redirect at the bottom is reached too late or behaves inconsistently. Move it above # BEGIN WordPress.

Case B: 500 right after a migration

# php -- BEGIN cPanel-generated handler, do not edit
<IfModule mime_module>
  AddHandler application/x-httpd-ea-php74 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit

# BEGIN WordPress
...
# END WordPress
Answer

A handler left by the previous host points to a PHP version that does not exist here. Remove the cPanel block and set the PHP version in hPanel.

Case C: ERR_TOO_MANY_REDIRECTS, site behind Cloudflare

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

The rule is correct, but Cloudflare SSL is set to Flexible, so the server always sees HTTP and keeps redirecting. Set Cloudflare SSL to Full (strict); curl -IL shows the same location repeating.


Adapted from the Dex outline, refocused on Apache and .htaccess. Linked points come from Hostinger tutorials; the rest (WordPress default block, rewrite flags, hardening snippets, lab cases) is general Apache and WordPress practice, not taken from our docs. The Mastery initiative page has no session plan for this topic, so this outline is a suggestion.