If your blog is on Apache, then, htaccess 301 redirect is the best way to handle permalink structure change and to keep the backlink traffic. But if you have subdomain then you can exclude them in the .htaccess 301 redirect by applying conditional redirect.
In the last article we discussed about the most effective permalink structure in WordPress. If you are not using the recommended structure, you can change that to the desired one. If you decided to change the structure then you need to take care of the 301 redirect so that you don’t loose the backlinks and search traffic. If your site or blog is on Apache and you decided to change your structure, then you can achieve the 301 redirect by adding appropriate directive in your .htaccess file.
There are two options to implement this. You can either use the mode-rewrite Apache module or the mod_alias Apache module. Mod_alias provides for mapping different parts of the host file system in the document tree and for URL redirection. Mod_rewrite is an Apache module that can be accessed from .htaccess files to perform all kinds of complicated URL manipulation.htaccess redirect 301

301 redirect using mode_alias module directives

You can use the Redirect or RedirectMatch to do a 301 redirect. Redirect can be used only for a single file. But RedirectMatch allows pattern matching. Thus in our case RedirectMatch is appropriate.
For example if your old permalink structure was /yyyy/mm/dd/%postname%/ and want to change to /%postname%/ then use the following redirect.

1
RedirectMatch 301 /d{4}/d{2}/d{2}/(.*) http://mydomain.com/$1

,where “mydomain.com” should be your domain name.

Subdomains and problem with RedirectMatch

If you use mode_alias directives then you cannot have a conditional redirect. For example, if you have a subdomain like “blog.example.com” and if you use the above RedirectMatch directive, then the hits to your subdomain also will be redirected to the main domain. To resolve this issue you can use the directives in the Mod_rewrite Apache module.

Conditional 301 redirect using Mod_rewrite directives

You can add conditional redirection using “RewriteCond” directive and add corresponding “RewriteRule” directive to do a 301 redirection.
For example the below redirect will exclude the subdomain “blog” from 301 redirect.

1
2
3
4
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^blog.mydomain.com$
RewriteRule ^/([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)$ http://www.mydomain.com/$4 [R=301,L]

, where “mydomain.com” should be your domain name.
If you don’t have a subdomain then you can use the mode_alias RedirectMatch directive. But if you have subdomains then you need to use Mod_rewrite directive to avoid unnecessary 301 redirect of the subdomains.
Learn more about Apache Module mod_alias
Learn more about Apache Module mod_rewrite

Sign up for our newsletter to receive great articles!
Placeholder Placeholder
Binu GeorgeWordPress Tipshow-to guides,htaccess 301,htaccess redirect 301,Mod_rewrite,mode_alias,productivity,redirect htaccess,RedirectMatch,RewriteCond,RewriteRuleIf your blog is on Apache, then, htaccess 301 redirect is the best way to handle permalink structure change and to keep the backlink traffic. But if you have subdomain then you can exclude them in the .htaccess 301 redirect by applying conditional redirect. In the last article we discussed...Tech-Knowledge that helps