add trailing slash to 404 url so it can be redirected

Situation: moved old WordPress site to new domain; same permalink structure, 301’d $1

Shortly before the move, the tags had been cleaned up; reduced from over 2000 tags to a dozen or so tags; altho the tags were merged on-site, obviously, lots of 404’s began as many of those old tags had been used on the site for years and widely indexed all over the internet.

Read More
  • i.e. (/tag/frank-sinatra/ or /tag/tony-bennett/ merged and redirected to /tag/singers/)

I have been redirecting 404’s as they occur via a combination of 404 Redirected Plugin http://wordpress.org/extend/plugins/404-redirected/ (logs and allows to create redirections), Google Webmaster Tools data (reports 404’s / crawl errors…), Broken Link Checker http://wordpress.org/extend/plugins/broken-link-checker/ and adding redirects and mod_rewrites to my htaccess as each solution seems most economical.

My permalink / canonical url structure (/%postname%/) uses a trailing slash, ie. /post-or-page-name/ or /tag/tag-name/ or /category/category-name/

That said, here is a problem I am presented with that I have been unable to solve myself with a redirection / mod_rewrite regex:

WordPress automatically redirects pages, posts, tags, categories missing the trailing trailing slash and I have created a considerable number of redirections BUT from the old tags to the new tags but lately I have encountered a slew of 404’s for old tag url requests that are missing the trailing slash, most of which I have already fixed as urls with a trailing slash. i.e.

  • /tag/frank-sinatra/ 301 –> /tag/singers/ = 200 Success
  • /tag/frank-sinatra = 404 Not Found

They are being 404’d before a trailing slash can be forced to the end of the url so they can be redirected

Is there a solution for this other than creating individual 301’s for each old tag url missing the trailing slash?

I have to believe this must be a pretty common problem but the work smart solution has eluded me so far!

Thanks those more knowledgeable than myself and generous enough to share a solution 🙂

Related posts

Leave a Reply

2 comments

  1. If all of your URLs end in a trailing slash, then one way to address this is to redirect those that do not have one. This will result in 2 301’s, but that is better than a 404.

    To do this add the following to the .htaccess file in the root directory of your site. Make sure these rules appear before other existing rules.

    RewriteEngine on
    RewriteBase /
    
    #if not an existing file
    RewriteCond %{REQUEST_FILENAME} !-f
    #redirect urls without a trailing slash 
    RewriteRule ^(.*[^/])$ $1/ [L,R=301] 
    

    If the URLs missing a trailing slash are all tag URLs, then change the last rule to

    RewriteRule ^(tag/.*[^/])$ $1/ [L,R=301]