In this article, we'll show you some of the common .htaccess rules that you might need when running a WordPress site.
Before we continue, please note that .htaccess rules cannot be used on Pagely-hosted sites that are set to NGINX-Only mode.
Default WordPress Rewrite Rules
The following are the standard rewrite rules that are included with WordPress by default:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Default WordPress Multisite (Subdirectory) Rewrite Rules
If you're running WordPress Multisite and have configured sub-sites to use a subdirectory, the default rules would be replaced with the following:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]
Default WordPress Multisite (Subdomain) Rewrite Rules
When configuring a mutisite WordPress installation to use subdomains, the rules would be changed to the following:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
Redirects with .htaccess
One of the most common uses for .htaccess rules is to redirect visitors from one page to another. For Pagely-hosted sites, we recommend adding redirects from within Atomic, but .htaccess rules can still be used for sites that are set to NGINX+Apache mode.
Note: These rules will not work for sites that are set to NGINX-Only mode.
Basic 301 (Permanent) Redirect
If content has been permanently moved to another location, this basic redirect can be used to redirect an old page to the new URL.
Redirect 301 /old-location/ /new-location/
Basic 302 (Temporary) Redirect
If you only need to temporarily redirect one location to another, a 302 redirect should be used instead of a 301.
Redirect 302 /old-location/ /new-location/
Redirecting All Non-WWW to WWW
It's a good idea to pick between non-www and www versions of your site, then redirect all traffic to the version you chose. Here's how you would redirect all content from example.com to www.example.com.
RewriteEngine on
RewriteBase /
RewriteCond %{http_host} ^example.com [nc]
RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,nc]
Redirecting HTTP to HTTPS
HTTPS should always be used when possible. Especially considering that SSL certificates can be obtained for free using Let's Encrypt.
Note: If you're doing this on a Pagely-hosted WordPress site, you can easily redirect to HTTPS from inside Atomic.
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}