htaccess Redirects After Changing WordPress Permalink Structure

For my WordPress site, I had been using the “Day and name” permalink structure which includes the year, month, and day followed by the post name, e.g. /2013/07/06/sample-post/. When I re-installed WordPress due to a data loss, I changed to the “Post name” permalink structure, e.g. /sample-post/. Since the site had been previously indexed by search engines under the old permalink structure, the search results returned the old “Day and name” URLs which were now generating 404 response codes (Not Found).

In order to return the more appropriate response code of 301 (moved permanently) and redirect users to the new URL, you can modify the htaccess file to include redirects. This can be done by either creating a redirect for each specific page/post that moved or you can use a regular expression to execute the redirect from the older permalink structure to the new one.

As an example, I would add the following RedirectMatch line near the top of the htaccess file:

RedirectMatch 301 ^/([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)$$4

If you’re using WordPress, then your htaccess file might have the following instructions:

Options All -Indexes

RewriteEngine On

RedirectMatch 301 ^/([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)$$4

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

# END WordPress

Leave a Comment