There are three ways to change the URL structure of your WordPress posts.
- By changing permalinks in the settings to post name
- Adding an entry to the
.htaccesfile using the rewrite. - Adding redirect rule to
.htaccess
The first method is pretty easy. Just go to Settings–> Permalinks and change to post option. You can also mention your custom structure.
The second method is by adding a rewrite rule as shown below. You need to add this to your .htaccess file.
For example, a URL like,
http://example.com/2016/06/example-file.html
Will redirect to
http://example.com/example-file.html
The redirect rule for .htaccess file is as follows.
RewriteRule ^[0-9]+/[0-9]+/(.*)$ https://example.com/$1 [NC,L,R]
The second method is useful when you are changing the URL structure for existing site with many posts. First, you need to change the permalink and you need to add the following in the .htaccess file. This will avoid any 404 errors for the links you have added in other posts or websites.
RedirectMatch 301 ^/([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)$ http://www.yourwebsite.com/$4
For more information, refer http://www.kevinmuldoon.com/redirect-wordpress-permalink-structure-postname/