I have a Wordpress website which contains the following URL pattern:


With the help of user w3dk, I have been able to permanent redirect it to:



RewriteEngine On
RewriteRule ^\d{4}/\d\d/\d\d/(.+) /$1 [R=301,L]

# 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

and kept the Permalink Settings in Wordpress as Post Name.

So far so good.

Now some newer posts on the website use a custom post type “items” bearing the url format:


Notice "items" in the url. For consistency, I now want to make my old urls of the same format so that the older urls use the custom post types "items".

Do I write another rewrite rule that now changes




If yes, how?


I now want to make my old urls of the same format so that the older urls use the custom post types "items".

Assuming you have already changed the URL structure in WordPress then in order to redirect the old URLs (for the benefit of search engines and user's bookmarks) you may be able to get away with just modifying your existing redirect (since it's "only" been 10 days since that was implemented), for example:

RewriteRule ^\d{4}/\d\d/\d\d/(.+) /items/$1 [R=301,L]

However, ideally you will need to redirect both /2017/01/30/sample-post/ (the "very old" URL format) and /sample-post/ (the now "old" URL format), particularly if your site gets significant traffic.

This is made a little more complex because /sample-post/ could be anything (we can't check whether "sample-post" is a valid WordPress page from .htaccess), but we can check if the URL starts with /items/ or not. So, for example, try:

RewriteCond %{REQUEST_URI} !^/items/
RewriteRule ^([^/]+)/$ /items/$1/ [R=301,L]

The RewriteRule pattern only matches a single path segment and the condition makes sure this is not /items/. (The trailing slash is currently enforced.)

This can be used together with the above redirect. The order should not matter.

You will need to make sure your browser cache is clear before testing, since the earlier 301s will have been cached by the browser. (They will also have been cached on your user's machine - but you can't do much about that unfortunately, since these are "permanent" redirects after all; they are not meant to change.)

Aside: You only need the RewriteEngine directive once in the file. It can occur multiple times, but the last instance wins and controls the entire file. Ideally, if you are hand coding your directives, you would just have one RewriteEngine On directive at the top of the file - logical, easier to read, less prone to error, etc. However, when you have different "plugins" writing to the .htaccess file, often the case with WordPress, it is not always practical/possible.

  • I am so glad you answered my question again :) I do not have a high traffic website. Infact it is just 300 visitors per day but these posts were created over 6 years ago. I am reviving the site. When I tried the first suggestion that is RewriteRule ^\d{4}/\d\d/\d\d/(.+) /items/$1 [R=301,L] it works for all old urls. However for the new urls with the custom post types "items" it now adds items two times making it somewebsite.com/items/items/sample-post I believe your second suggestion using RewriteCond applies only to urls not already having "items" in it right? – CuriousDev Feb 10 '17 at 11:35
  • Not sure why you would get "items" twice like that, unless there is a conflict somewhere else (ie. WordPress)? Can you confirm that the requested URL is /items/sample-post/? Check the network traffic... what redirects do you see? Presumably /items/items/sample-post/ results in a 404? – MrWhite Feb 10 '17 at 16:51
  • 1
    Yes, the RewriteCond directive makes sure that the following redirect only happens when the URL does not already start /items/. In fact, any request that contains more than one path segment (eg. /foo/bar/) would also be excluded, regardless of whether it contains "items". – MrWhite Feb 10 '17 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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