We have a problem where a site has got some odd URLs indexed by Google.

This is the incorrect path:


This is the correct path:


I was told this would do it:

RewriteRule ^shop/[^/]+/[^/]+/([^/]+)/?$ /shop/$1 [R=301,L]

But it doesn't. When I refresh the error page, it just refreshes. The /shop/$1 at the end, just seems not to take into account what it now needs to be.

In short, Permalinks are fine. All product links are fine on the site. But as Google has these indexed, it's canonicals are therefore wrong, as they differ to the "longer" URL.

So we want to nail them all down to the shorter - category-only - version.

What would the correct .htaccess rule be, and how is it written?

1 Answer 1


When I refresh the error page, it just refreshes.

If the rule has been put in the correct place in the .htaccess file (ie. near the top of the file before the existing WordPress code block) then you shouldn't be seeing an "error page" at all (if the redirect occurred correctly).

(If you place this rule later in the .htaccess file, after the WordPress code block, then it won't do anything as it's not going to be processed.)

However, the rule you've posted is not correct either (at least in terms of the example URL you've posted). That rule would redirect the (incorrect) URL /shop/mens/jeans/jean-product/ to /shop/jean-product (also incorrect), note the absence of /mens/jeans (when only /jeans should be omitted) and the removal of the trailing slash.

To redirect /shop/mens/jeans/jean-product/ to /shop/mens/jean-product/ (ie. removing the 3rd path segment) then the rule should look like this instead:

RewriteRule ^(shop/[^/]+)/[^/]+/([^/]+)/?$ /$1/$2 [R=301,L]

From your example, the $1 backreference would contain shop/mens and $2 contains jean-product (note the absence of the front and trailing slashes). No need to repeat shop in the substitution string since we are capturing that from the requested URL.

Note that you must test first with a 302 (temporary) redirect to avoid potential caching issues. 301 (permanent) redirects are cached persistently by the browser (and possibly intermediate caches) so can make testing problematic. You need to clear your browser cache before testing.

Note the difference in the capturing subpatterns between your original rule and the rule I posted above:

# Your original rule
#                 |----------| (Part that is removed)
RewriteRule ^shop/[^/]+/[^/]+/([^/]+)/?$ /shop/$1 [R=301,L]
# New rule
#                         |----| (Part that is removed) 
RewriteRule ^(shop/[^/]+)/[^/]+/([^/]+)/?$ /$1/$2 [R=301,L]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.