Mobile app version of vmapp.org
Login or Join
Angie530

: Two Permanent Redirects using .htaccess I have a Wordpress website which contains the following URL pattern: http://www.somewebsite.com/2017/01/30/sample-post/ With the help of user w3dk, I have been

@Angie530

Posted in: #301Redirect #Htaccess #Seo #Wordpress

I have a Wordpress website which contains the following URL pattern:
www.somewebsite.com/2017/01/30/sample-post/

With the help of user w3dk, I have been able to permanent redirect it to:
www.somewebsite.com/sample-post/

using

RewriteEngine On
RewriteRule ^d{4}/dd/dd/(.+) / [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]
</IfModule>

# 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:
www.somewebsite.com/items/sample-post/

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
www.somewebsite.com/sample-post/

to
www.somewebsite.com/items/sample-post/

If yes, how?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Angie530

1 Comments

Sorted by latest first Latest Oldest Best

 

@Ogunnowo487

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}/dd/dd/(.+) /items/ [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// [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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme