Per Page Permalink Structure for Page Post Type

I am trying to Re-Write Page URL's so that they END with .htm

I was able to do this using...

/* Add .htm extension to Page URL Links */
add_action('init', 'htm_page_permalink', -1);
function htm_page_permalink() {
    global $wp_rewrite;
    if ( !strpos($wp_rewrite-get_page_permastruct(), '.htm')){
        $wp_rewrite-page_structure = $wp_rewrite-page_structure . '.htm';
    }
}

/* Remove the trailing slash/ on Pages */
add_filter('user_trailingslashit', 'no_page_slash',66,2);
function no_page_slash($string, $type){
    global $wp_rewrite;
    if ($wp_rewrite-using_permalinks()  $wp_rewrite-use_trailing_slashes==true  $type == 'page'){
        return untrailingslashit($string);
    }else{
        return $string;
    }
}

But then I ran into a problem where I am using my Blog post list on a Page named Blog. I need this page to NOT end with .htm

So I then used this code to change the Page Links to filter out the .htm on my blog page...

/* Filter .htm out of Blog Page Links */
function blog_page_url($content) {
    $current_path = '/blog.htm';
    $new_path = '/blog/';
    $content = str_replace($current_path, $new_path, $content);
    return $content;
}
add_filter('_get_page_link', 'blog_page_url');

And I used this Re-write rule to make /blog/ load the Blog Page ...

/* Setup Custom Re-write for the Blog Page */
add_action( 'init', 'blog_page_rewrite' );
function blog_page_rewrite(){
    add_rewrite_rule('blog', 'index.php?pagename=blog','top' );
    //add_rewrite_rule('blog/(.*?)/', 'index.php?p=$matches[1]','top' );
}

But now I have the problme that any URL with the /blog/ in it is showing the Blog page.

This means my actual blog post which are set up in WP Permalink settings like this...

/blog/%postname%/ are not working, instead they always show the Blog page which list all the blog posts.

In my blog_page_rewrite() function above you can see I had tested an extra re-write rule to hopefully work with the Blog posts but it had no affect.

Topic rewrite-rules url-rewriting urls Wordpress

Category Web


Is your Blog page the one that's set in the back end at Settings > Reading > Posts page? If so, try checking for that in your first function:

/* Add .htm extension to Page URL Links */
add_action('init', 'htm_page_permalink', -1);
function htm_page_permalink() {
    global $post;
    if( get_option( 'page_for_posts' ) == $post->ID ) {
         // don't add .htm to this page
         return;
    }
    global $wp_rewrite;
    if ( !strpos($wp_rewrite->get_page_permastruct(), '.htm')){
        $wp_rewrite->page_structure = $wp_rewrite->page_structure . '.htm';
    }
}

The Option Reference pointed me at page_for_posts.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.