Title: Changing permalink structure
Last modified: February 23, 2017

---

# Changing permalink structure

 *  [peoy](https://wordpress.org/support/users/peoy/)
 * (@peoy)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/changing-permalink-structure-10/)
 * Hello,
 * This has probably been asked somewhere else, and is not purely related to the
   Sparkling theme, but anyway…
 * If I change the permalink structure from “day and name” to “post name”, without
   changing any of the slugs themselves, it should some way be possible to automatically
   retry getting the page (at least up to 2 times) before giving the user the 404-
   page.
 * For example
    Old URL [http://some.site.com/2017/02/23/that-really-interesting-post/](http://some.site.com/2017/02/23/that-really-interesting-post/)
 * Changed to (unless this change made a duplicate slug, which WP then adds a number
   to)
    [http://some.site.com/that-really-interesting-post/](http://some.site.com/that-really-interesting-post/)
 * Before showing the user the 404-page, I want to try out these URLs in order:
   
   [http://some.site.com/2017/02/that-really-interesting-post/](http://some.site.com/2017/02/that-really-interesting-post/)
   [http://some.site.com/that-really-interesting-post/](http://some.site.com/that-really-interesting-post/)
 * Which file should be edited (preferrably in a child theme) to accomplish this?
   
   404.php is the wrong answer (as it only is used for the content of the 404-page).

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [Vinod Dalvi](https://wordpress.org/support/users/vinod-dalvi/)
 * (@vinod-dalvi)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/changing-permalink-structure-10/#post-8840038)
 * Hi [@peoy](https://wordpress.org/support/users/peoy/),
 * I hope you are well today and thank you for your question.
 * You can just redirect users from old URL to new one using following plugin.
 * [https://wordpress.org/plugins/redirection/](https://wordpress.org/plugins/redirection/)
 * Best regards,
    Vinod Dalvi
 *  Thread Starter [peoy](https://wordpress.org/support/users/peoy/)
 * (@peoy)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/changing-permalink-structure-10/#post-8840310)
 * That plugin is for redirecting known pages that have been moved from one URL 
   to another.. Maybe it is configurable to do what I did in the very beginning 
   of 404.php (it works, but the problem is that Apache already has sent headers
   and content before that redirect-header which is sent now)
 * $parts = explode(“/”,trim($_SERVER[‘REDIRECT_URL’],”/”));
    if (count($parts) 
   > 3) { $new = “[http://](https://wordpress.org/support/topic/changing-permalink-structure-10/?output_format=md){
   $_SERVER[‘SERVER_NAME’]}/{$parts[3]}/”; if ( the_slug_exists(“{$parts[3]}”)) {
   header(“HTTP/1.1 301 Moved Permanently”); header(“Location: $new”); } }
 * function the_slug_exists($post_name) {
    global $wpdb; if($wpdb->get_row(“SELECT
   post_name FROM wp_posts WHERE post_name = ‘” . $post_name . “‘”, ‘ARRAY_A’)) {
   return true; } else { return false; } }
 * As the first step trying to find the requested article is automatic (from /year/
   month/day/postname to /year/month/postname) I removed a part of the code, also,
   instead of multiple redirections, I check for the existanse of the requested 
   slug first, and then redirect.
    -  This reply was modified 9 years, 2 months ago by [peoy](https://wordpress.org/support/users/peoy/).
      Reason: updated code for redirecting
 *  [Vinod Dalvi](https://wordpress.org/support/users/vinod-dalvi/)
 * (@vinod-dalvi)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/changing-permalink-structure-10/#post-8844504)
 * If you don’t want to redirect then not sure what you want to achieve so please
   advise.
 *  Thread Starter [peoy](https://wordpress.org/support/users/peoy/)
 * (@peoy)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/changing-permalink-structure-10/#post-8849312)
 * I want to redirect only the 404’s caused by changing the permalink structure (
   WP settings -> Permalinks).
 * In case someone changes permalinks from the format “Day and name”, to a more 
   simple (shorter) format, like in my case “Post name”, all indexed post links 
   on Google (and all post links saved and linked by visitors) will generate 404s.
 * As I described above, I need a way to try to redirect to the correct post, before
   throwing the 404-page up in the face of the visitor.
 * The code in my reply will do this, but probably not in the best way. Part of 
   the functionality I’m looking for seems to be built-in into WordPress or the 
   Sparkling theme, to redirect from “Day and name” to “Month and name”, but not
   the last step to go directly to the post name format.
 *  [Vinod Dalvi](https://wordpress.org/support/users/vinod-dalvi/)
 * (@vinod-dalvi)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/changing-permalink-structure-10/#post-8853577)
 * Ok. You can try redirecting it using htaccess rules as described on the following
   pages.
 * [https://my.bluehost.com/cgi/help/htaccess_redirect](https://my.bluehost.com/cgi/help/htaccess_redirect)
   
   [https://mediatemple.net/community/products/grid/204643080/how-do-i-redirect-my-site-using-a-htaccess-file](https://mediatemple.net/community/products/grid/204643080/how-do-i-redirect-my-site-using-a-htaccess-file)
   [https://help.dreamhost.com/hc/en-us/articles/215747748-How-can-I-redirect-and-rewrite-my-URLs-with-an-htaccess-file-](https://help.dreamhost.com/hc/en-us/articles/215747748-How-can-I-redirect-and-rewrite-my-URLs-with-an-htaccess-file-)
   [http://www.inmotionhosting.com/support/website/redirects/setting-up-a-301-permanent-redirect-via-htaccess](http://www.inmotionhosting.com/support/website/redirects/setting-up-a-301-permanent-redirect-via-htaccess)

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Changing permalink structure’ is closed to new replies.

 * ![](https://i0.wp.com/themes.svn.wordpress.org/sparkling/2.4.11/screenshot.png)
 * Sparkling
 * [Support Threads](https://wordpress.org/support/theme/sparkling/)
 * [Active Topics](https://wordpress.org/support/theme/sparkling/active/)
 * [Unresolved Topics](https://wordpress.org/support/theme/sparkling/unresolved/)
 * [Reviews](https://wordpress.org/support/theme/sparkling/reviews/)

## Tags

 * [permalinks](https://wordpress.org/support/topic-tag/permalinks/)

 * 5 replies
 * 2 participants
 * Last reply from: [Vinod Dalvi](https://wordpress.org/support/users/vinod-dalvi/)
 * Last activity: [9 years, 2 months ago](https://wordpress.org/support/topic/changing-permalink-structure-10/#post-8853577)
 * Status: not resolved