WordPress .htaccess Generator, Default Code and Snippets

The .htaccess file is one of the most powerful configuration files on any WordPress site. This small text file sits in your site’s root folder and tells the Apache web server how to behave — handling your permalinks, forcing HTTPS, blocking attackers, speeding up your site, and setting up redirects. This guide covers everything: what the WordPress .htaccess file is, where to find it, how to create and edit it safely, the default WordPress code, and a library of ready-to-use snippets for security, performance, and redirects.

⚠️
Back up before editing

The .htaccess file is read on every request. A single syntax error can take your whole site down with a 500 error. Always keep a copy of the working file before making changes — if something breaks, just re-upload it. Add one rule at a time and test after each.

Build your WordPress .htaccess

.htaccess builder for WordPress

Pick the rules you want and copy a complete .htaccess, with the WordPress block in the right place. Paste your current file to keep its rules and check it for common problems.

WordPress rules
Keep / when the site opens at the domain root. Use /blog/ for example.com/blog/.
Security
Speed
Headers

For Apache and LiteSpeed; Nginx ignores .htaccess files. Rules tested on Apache 2.4. Nothing you paste leaves your browser. Source: copahost.com

What is the .htaccess file?

The name .htaccess comes from “HyperText Access.” It’s a distributed configuration file for the Apache web server, working at the directory level: a rule you place in the root folder applies to your whole site, and you can also place a separate .htaccess inside a subfolder to affect only that directory. WordPress uses it most notably to handle pretty permalinks — the clean, readable URLs like yoursite.com/about instead of yoursite.com/?p=42.

With the right directives you can force HTTPS, block IP addresses, deny access to sensitive files, set up redirects, prevent hotlinking, enable compression, and much more. It’s plain text, so you can edit it in any text editor.

💡
Apache only — not Nginx

The .htaccess file only works on Apache servers (and compatible ones like LiteSpeed). Nginx does not use .htaccess — the same rules have to be set in the server’s main configuration instead. If your host runs Nginx, the snippets below won’t apply.

Where is the .htaccess file in WordPress?

Your WordPress .htaccess lives in the root directory of your installation — usually the public_html folder, alongside wp-config.php and wp-admin. Because the name starts with a dot, it’s a hidden file: if you don’t see it, enable “show hidden files” (dotfiles) in your File Manager or FTP client.

You can reach it through your hosting File Manager (in cPanel) or an FTP client like FileZilla. If WordPress is installed in a subfolder, the file is inside that folder.

How to create the WordPress .htaccess file

If there’s no .htaccess in your root, WordPress usually creates one automatically. The simplest way to trigger that: in your dashboard, go to Settings → Permalinks and click Save Changes (without changing anything). WordPress regenerates the default file.

If it can’t (a file-permissions issue), create it manually: open a plain-text editor, save an empty file named exactly .htaccess (with the leading dot, no extension), set “Save as type” to All Files so it isn’t saved as .txt, and upload it to public_html.

The default WordPress .htaccess code

Here’s the standard block WordPress adds to make permalinks work. If your file is corrupted or your links suddenly 404, you can restore this:

# 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

Never edit anything between the # BEGIN WordPress and # END WordPress markers — WordPress rewrites that block automatically. Always add your custom rules outside (above or below) those markers.

Restoring .htaccess and the 500 error

The .htaccess file is the number-one cause of the 500 Internal Server Error on WordPress. If your site goes down right after you edit it, the file is the prime suspect. The fastest test: rename the file (e.g. to .htaccess_old) via File Manager and reload the site. If it comes back, the problem was in the file — regenerate a clean one via Settings → Permalinks → Save Changes, or paste the default block above.

Essential .htaccess snippets for WordPress

Here are the most useful .htaccess rules for WordPress. Copy only the ones you need, always with a backup first, and place them outside the # BEGIN/END WordPress markers. To build the whole file at once, use the .htaccess builder at the top of this page.

Protect the wp-config.php file. Your most sensitive file — block all web access to it:

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

Learn more in our full wp-config.php guide.

Protect multiple sensitive files at once (wp-config, .htaccess, debug.log):

<FilesMatch "^(wp-config\.php|\.htaccess|\.htpasswd|debug\.log)$"> Require all denied </FilesMatch>

Disable PHP execution in the uploads folder. Stops a malicious script uploaded to wp-content/uploads from running (create this .htaccess inside that folder):

<FilesMatch "(?i)\.(php[0-9]?|pht|phtml|phar)$"> Require all denied </FilesMatch>

Block access by IP. Deny a specific abusive IP (replace 203.0.113.45 with the address you want to block):

<RequireAll> Require all granted Require not ip 203.0.113.45 </RequireAll>

Force HTTPS (redirect HTTP to HTTPS). Once you have an SSL certificate installed, you’ll want every visitor on the secure https:// version of your site — both for security and because Google uses HTTPS as a ranking signal. This rule redirects all HTTP traffic to HTTPS with a permanent (301) redirect:

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

Add it outside the # BEGIN/END WordPress markers. Two notes: if your site sits behind a proxy or CDN like Cloudflare, forcing HTTPS at both levels can cause a redirect loop — see our guide to ERR_TOO_MANY_REDIRECTS. And WordPress can also enforce HTTPS through the Site Address setting; use one method, not both, to avoid conflicts.

Force www or non-www (canonical domain). Your site shouldn’t answer on both www.yoursite.com and yoursite.com — search engines see them as two separate sites, splitting your SEO value. Pick one canonical version and redirect the other to it. To redirect www to non-www:

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

Or, to do the opposite and redirect non-www to www:

RewriteEngine On RewriteCond %{HTTP_HOST} ^yoursite\.com [NC] RewriteRule ^(.*)$ https://www.yoursite.com/$1 [L,R=301]

Replace yoursite.com with your domain, and pick only one of the two — running both creates a loop. For more on permanent redirects, see our HTTP 301 guide.

Set up a 301 redirect. Point an old URL to a new one:

Redirect 301 /old-page https://yoursite.com/new-page

Enable Gzip compression. Shrinks files sent to the browser for faster loading:

<IfModule mod_deflate.c> AddOutputFilterByType DEFLATE text/html text/plain text/css AddOutputFilterByType DEFLATE application/javascript text/javascript AddOutputFilterByType DEFLATE application/json application/xml </IfModule>

Leverage browser caching. Tell browsers to cache static assets:

<IfModule mod_expires.c> ExpiresActive On ExpiresByType image/jpeg "access plus 1 year" ExpiresByType image/png "access plus 1 year" ExpiresByType image/webp "access plus 1 year" ExpiresByType text/css "access plus 1 month" ExpiresByType application/javascript "access plus 1 month" </IfModule>

Prevent image hotlinking. Stop other sites from embedding your images and stealing bandwidth (replace yoursite.com):

RewriteEngine On RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !^https://(www\.)?yoursite\.com [NC] RewriteRule \.(jpg|jpeg|png|gif|webp)$ - [F]

Other common uses

The .htaccess file can do much more. A few common tasks have their own dedicated guides:

WordPress hosting built for speed and security

The .htaccess file is one layer. Copahost runs on Apache/LiteSpeed with cPanel access, free SSL, and support in your corner — so you can tune redirects, caching, and security without breaking your site.

See web hosting plans

Frequently asked questions

Where is the .htaccess file in WordPress?
In the root directory of your install, usually the public_html folder, alongside wp-config.php. It’s a hidden file (the name starts with a dot), so you may need to enable “show hidden files” in your File Manager or FTP client to see it.

My WordPress site has no .htaccess file. How do I create one?
Go to Settings → Permalinks and click Save Changes — WordPress regenerates the file automatically. If that fails due to permissions, create an empty file named exactly .htaccess in a text editor and upload it to public_html.

Why did editing .htaccess cause a 500 error?
A syntax error in .htaccess is the most common cause of a 500 Internal Server Error on WordPress. Rename the file to .htaccess_old to confirm, then regenerate a clean one via Settings → Permalinks → Save Changes.

Does .htaccess work on Nginx?
No. The .htaccess file is specific to Apache (and compatible servers like LiteSpeed). Nginx doesn’t use it — equivalent rules must be added to the Nginx server configuration instead.

Where do I add custom rules in the WordPress .htaccess?
Always outside the # BEGIN WordPress and # END WordPress markers — either above or below that block. WordPress automatically rewrites everything between those markers, so custom rules placed inside would be lost.

How do I generate a WordPress .htaccess file?
Use the .htaccess builder at the top of this page. Pick the rules you want, such as wp-config.php protection, blocking PHP in uploads, compression, browser caching and security headers, and it writes a complete file with the WordPress block after your custom rules. Paste your current file into it to keep its rules and catch common problems, like a duplicated WordPress block.

Related guides

The .htaccess file connects to several WordPress topics — see our guides on the wp-config.php file, WordPress login and wp-admin, the 500 Internal Server Error, and our hub of common WordPress errors.

Conclusion

The WordPress .htaccess file is small but mighty: it powers your permalinks, secures your most sensitive files, speeds up your site with compression and caching, and handles redirects that protect your SEO. You don’t need to be a developer to use it — just back up before editing, add one rule at a time, test as you go, and keep custom rules outside the WordPress markers. With the builder at the top of this page or the snippets in this guide, you can harden and optimize your site directly from a single text file.

Share the Post:
Picture of Gustavo Gallas

Gustavo Gallas

Graduated in Computing at PUC-Rio, Brazil. Specialized in IT, networking, systems administration and human and organizational development​. Also have brewing skills.