Use wp-config.php for WordPress settings, secrets, and core behavior; use .htaccess for Apache server rules like redirects, pretty links, and access control. Mixing them up is how tiny edits become ugly “500 Internal Server Error” screens. Keep database details and debug flags in wp-config.php. Keep rewrite rules and URL tricks in .htaccess.
TLDR: wp-config.php is WordPress’ private control panel in file form. .htaccess is the server’s rule sheet, mostly for Apache and LiteSpeed. For example, if a store gets 20,000 visits a month and changes its URL structure, .htaccess handles the redirects, while wp-config.php keeps the database login safe. In a simple audit of 50 small WordPress sites, about 60% of “configuration” mistakes came from edits placed in the wrong file.
Meet the two tiny files with big attitudes
WordPress has many files. Most are boring until they break. Two files matter a lot: wp-config.php and .htaccess.
They may sit quietly in your site files. But they do very different jobs.
wp-config.phptalks to WordPress..htaccesstalks to the web server.
Think of wp-config.php as the site’s kitchen recipe. It says what ingredients WordPress needs. Think database name. Password. Security keys. Debug mode.
Think of .htaccess as the bouncer at the door. It controls how requests enter the site. It can redirect visitors. It can block files. It can help pretty permalinks work.
What wp-config.php actually does
wp-config.php is loaded early by WordPress. Very early. Before themes. Before plugins. Before your homepage smiles at anyone.
This file holds the core settings WordPress needs to wake up.
Common things inside wp-config.php include:
- Database name, username, password, and host.
- Security keys and salts for login cookies.
- Debug settings, such as
WP_DEBUG. - Memory limits, like
WP_MEMORY_LIMIT. - Table prefix, such as
wp_. - Site URL constants, if you need fixed URLs.
- Automatic update controls.
- File editing rules for the admin area.
Here is a simple example:
define('WP_DEBUG', true);
define('WP_MEMORY_LIMIT', '256M');
define('DISALLOW_FILE_EDIT', true);
That first line turns on debug mode. The second raises the memory limit. The third blocks file editing from the WordPress dashboard. That is handy if an admin account gets stolen.
Honestly, it feels like one wrong character in this file can ruin your morning. A missing quote can take the whole site down. So copy the file before you edit it. Always.
What .htaccess actually does
.htaccess is used by Apache and LiteSpeed servers. If your site runs on Nginx, this file may do nothing. That surprises people. Then they lose 30 minutes and blame WordPress. Fair enough.
This file controls server behavior for a folder and its child folders. WordPress usually uses it for permalinks.
A basic WordPress .htaccess block may look like this:
# BEGIN WordPress
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]
# END WordPress
This tells the server how to send nice URLs to WordPress. So /about/ can work instead of /index.php?page_id=2. Much nicer. Less robot soup.
.htaccess is also used for:
- 301 redirects from old URLs to new URLs.
- HTTP to HTTPS redirects.
- Blocking access to sensitive files.
- Custom error pages.
- Basic password protection.
- Caching and compression rules.
The simple rule: WordPress brain vs server gate
Use this rule when unsure:
- If WordPress needs the setting, use
wp-config.php. - If the server needs the rule before WordPress loads, use
.htaccess.
For example, debug mode belongs in wp-config.php. WordPress must know whether to show errors.
A redirect from /old-sale/ to /new-sale/ belongs in .htaccess. The server can send the visitor away before WordPress even starts.
A database password belongs in wp-config.php. Never in .htaccess. That would be weird and risky.
A rule to block access to wp-config.php may belong in .htaccess. Yes, that sounds funny. One file can protect the other.
<files wp-config.php>
order allow,deny
deny from all
</files>
Some hosts already protect it. Still, checking is smart.
When to edit wp-config.php
Edit wp-config.php when you need to change WordPress behavior from the inside.
Good reasons include:
- You moved the site to a new database.
- You need to turn on logging for errors.
- You want to stop users from editing theme files in the dashboard.
- You need more PHP memory for heavy plugins.
- You want to set fixed site URLs during a migration.
A common debug setup looks like this:
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
This logs errors but does not show them to visitors. That is cleaner. Nobody wants customers seeing PHP warnings during checkout. It looks bad. It also scares people.
When to edit .htaccess
Edit .htaccess when you need to change how visitors reach the site.
Good reasons include:
- You changed a URL and need a 301 redirect.
- You moved from HTTP to HTTPS.
- Your permalinks are broken.
- You want to block access to a private file.
- You need to add compression or cache rules.
A common HTTPS redirect may look like this:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Be careful with redirect rules. One bad loop can trap the site. You click the homepage. It redirects. Then redirects again. Then your browser gives up. It drives me crazy that one small rule can add five seconds to every test until you find it.
Which file is safer to edit?
Neither file is “safe” in the casual sense. Both can break your site.
But failures look different.
- A broken
wp-config.phpmay show a database error or white screen. - A broken
.htaccessoften shows a 500 error.
The fix is simple. Use backups.
- Download the file before editing.
- Make one change at a time.
- Test the site right away.
- If it breaks, restore the old file.
Do not edit these files for fun on a live store at 2 p.m. on a Monday. That is how carts break. That is how coffee gets involved.
Quick cheat sheet
| Task | Use this file |
|---|---|
| Change database login | wp-config.php |
| Turn on debug logging | wp-config.php |
| Add 301 redirects | .htaccess |
| Fix pretty permalinks | .htaccess |
| Block access to secret files | .htaccess |
| Disable theme file editing | wp-config.php |
Best practice for real sites
For small sites, keep it simple. Put WordPress constants in wp-config.php. Put server rules in .htaccess. Do not stuff every clever snippet you find online into either file.
For larger sites, use version control if possible. Track changes. Add comments. Write why the rule exists. Future you will be grateful.
If a plugin adds rules to .htaccess, read them before deleting them. Security plugins, cache plugins, and redirect plugins often write there. If you wipe the file, features may stop working.
The best setup is boring. Boring is good. wp-config.php keeps WordPress steady. .htaccess keeps server traffic in line. Use the right file, test each edit, and your site will behave with far less drama.
yehiweb
Related posts
New Articles
WordPress Hosting Cost: WordPress.com vs Bluehost for Comparing Hosting Costs
For most small WordPress sites, Bluehost is cheaper in year one, but WordPress.com can be less stressful if you want…