How to Redirect WordPress URLs with Apache .htaccess Rewrite Rules

How to Redirect WordPress URLs with Apache .htaccess Rewrite Rules

.htaccess files, also known as distributed configuration files, are use to manage configurations for websites that are running on Apache webserver.

Complete List of Apache .htaccess Rewrite Rules for WordPress

If your WordPress installation is running on Apache, then you will need to learn how to change the web server configuration for WordPress. In this tutorial, we will show you how to do the following redirect with WordPress.

  • Default .htaccess Rewrite Rules for WordPress
  • 301 Permanent Redirect
  • 302 Temporary Redirect
  • Force Non-www URL to www
  • Redirect www URLs to Non-www
  • Force HTTP to HTTPS

Default .htaccess Rewrite Rules for WordPress

WordPress installation usually comes with a default .htaccess in the root directory. But if your do not have one, you can copy the following into a text editor and save the file as .htaccess, and then upload the file to the root root directory of your WordPress installation.


# 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

htaccess Rule for WordPress URL 301 Permanent Redirect

301 is a HTTP Status Code, it tells the search engine that a URL is permanently moved to another URL. See below code snippet on how to do a 301 permanent redirect with a WordPress URL:


Redirect 301 /old-page.html http://www.yourwebsite.com/new-page.html

Rewrite Rule for WordPress URL 302 Temporary Redirect

Similar to 301, 302 is also a HTTP Status Code that tells search engine that a URL is moved to another URL, but the redirection is temporary. See below code snippet on how to do a 302 temporary redirect with a WordPress URL:


Redirect 302 /old-page.html http://www.yourwebsite.com/new-page.html

Force WordPress Non-www URL to www

The following .htaccess rewrite rule will redirect all non-www URLs to www.


RewriteEngine on
RewriteCond %{HTTP_HOST} ^youir-site.com [NC]
RewriteRule ^(.*)$ http://www.your-site.com/$1 [L,R=301,NC]

Force www WordPress URLs to Non-www

Using the following WordPress .htaccess rewrite rule will redirect all www URLs to non-www.


RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.your-site.com [NC]
RewriteRule ^(.*)$ http://your-site.com/$1 [L,R=301]

Force WordPress To Use HTTPS

Below .htaccess code snippet will force all visitors of your site to be on HTTPS.


RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

You May Also Like