Redirect your WordPress site with htaccess

Dec 22, 2014

Redirect your WordPress site with htaccess

Dec 22, 2014

Redirection is the process of forwarding a URL request to another URL. There are many cases where redirection plays an important role – moving your site to a new domain, redirecting a single page, moving to a different file extension, redirecting www to non www version of the site, and many more. The redirection can be permanent (type 301) or temporary (type 302). Type 301 (permanent redirection) is recommended in most cases as it passes on your SEO rankings to the new location with nearly no impact. Type 302 (temporary redirection), on the other hand, is only used in exceptional cases like site maintenance as it impacts your SEO rankings in a big way.

301 redirect your WordPress site
302 redirect your WordPress site

You can easily redirect your WordPress site with htaccess. Typically the redirect directive looks like this –

redirect old-page-or-url new-location

redirect – the redirect command

old-page-or-url – the old location that needs to be redirected

new-location – the new file or url to redirect to

Redirecting to a new domain

The following rule will redirect all the traffic on your old site to the new one. Needless to say, you’ve got to add this directive in the root htaccess file of your old site.

Redirect 301 /

Moving a single page

The following rule makes for seamless browsing of your site whenever you move a page to a new location –

Redirect 301 /oldpage.html

Redirecting www to non www version

Most site owners force either www or non-www versions of their sites as the preferred one. This approach of using one form is known to benefit SEO. However, you still want both versions to lead to your site. The easiest way to achieve this is by adding a redirect rule to your htaccess file.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.mysite\.com [NC] RewriteRule ^(.*)$$1 [L,R=301]

The above lines ensure than whenever the condition (www version) is met, the URL rewritten to the non-www version.

In case you want to reverse this process, i.e. redirect non-www version to www, you can do that too by adding the following lines –

RewriteEngine on
RewriteCond %{HTTP_HOST} ^ [NC] RewriteRule ^(.*)$$1 [L,R=301,NC]

In case you have https enabled, remember that the rewritten URL must also include https. Under no circumstance should you rewrite a secure URL to a non-secure version or vice versa.

Setting up redirect rules can be a little tricky. However, with this article you should have a better idea of what redirects are all about. Have any queries? Drop me a note.

Share via
Copy link