Search code examples
regexwordpressapache.htaccessmod-rewrite

Wordpress: How can I redirect all wordpress tags from subdirectory (subfolder) to another one


I changed my permalinks and it working fine, but I need to redirect old tag folder to archives/tag to not lose links which google indexed

below is my .htaccess file in the root of my recipes site http://foodonia.com

# Use PHP5.4 as default
AddHandler application/x-httpd-php54 .php

# 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>

Example: http://foodonia.com/tag/fudge/ should redirect to http://foodonia.com/archives/tag/fudge

I tried to add each one of blow codes but redirect not working

#RewriteRule tag/(.*) archives/tag/$1 [NC,L]

#RewriteRule tag/$ /archives/tag/$1 [NC,L]

#RewriteRule tag/ /archives/tag [L]

even I tried redirect 301 ^/tag/fudge/ http://foodonia.com/archives/tag/fudge/ but it didn't work

I try to search for solution but can't find similar post


Solution

  • You need to use rules in correct order i.e. to use this 301 first before other WP rules.

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    
    RewriteRule (tag/.*)$ /archives/$1 [NC,NE,L,R=301]
    
    RewriteRule ^index\.php$ - [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>