I am working on an imported .html site that has been using the "html on pages" plugin to rewrite the URLs to end in .html. (this is for maintaining inbound links). Now I am also trying to integrate it with a subdomain blog (domain.com/blog/) that also needs to maintain inbound links.
So far everything is working correctly except the blog archive page. The permalinks are set to (/blog/%postname%/) and the URLs are correct for both pages and posts. The only issue is the page set to frontpage (/blog/)is being rewritten to /blog.html/ so links to (domain.com/blog/) are getting a 404.
There doesn't seem to be an easy way to exclude the page from the .html rewrite, (see code below). I tried using a conditional tag, but the plugin hooks to 'init' so it is loading before the conditionals. Is there another way to exclude that page from being rewritten? Or is there some other (hopefully simpler) way to achieve a blog archive at (domain.com/blog/)?
The URL of the development site is http://wpdev.lookyounger.net. Beware that it is a plastic surgeons site and has some graphic imagery.
Code from the .html on pages plugin*:
function html_page_permalink() {
global $wp_rewrite;
if ( !strpos($wp_rewrite->get_page_permastruct(), '.html')){
$wp_rewrite->page_structure = $wp_rewrite->page_structure . '.html';
}
}
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;
}
}
I copied this from the plugin into functions.php to avoid the use of the plugin. It is working properly.
John Cotton answers:
I'm not sure this will work, but you could try overriding the rewrites with a global rewrite just for that url...
Add this to your functions.php
function my_insert_rewrite_rules( $rules ) {
$newrules = array();
$newrules['^blog/$'] = 'index.php?post_type=post';
return $newrules + $rules;
}
add_filter('rewrite_rules_array','my_insert_rewrite_rules');
Sirmontegu comments:
John, this had no effect. Would this override catch the already rewritten /blog.html/ URL? Or would it act before the .html rewrites?
John Cotton comments:
Did you re-save your permalinks? Sorry, I should have said that.
Go to Settings > Permalinks and save whatever you've got currently (ie you don't need to change, just save) and it will work.
John Cotton comments:
<blockquote>Would this override catch the already rewritten /blog.html/ URL? Or would it act before the .html rewrites?</blockquote>
Yes. By adding the new rule at the top of the list, it will get matched first and thus be the one that takes effect.
John Cotton comments:
You might want to try
$newrules['^blog$'] = 'index.php?post_type=post';
as well ie without the trailing backslash. Add the above as an extra line above or below the current, doesn't matter.
Sirmontegu comments:
Thanks so much John, In the end ['^blog$'] without the trailing slash did the trick. And as an added bonus /blog.html/ still works as well.
After a full day hunting wild geese (mostly and errant space in a key function that was preventing the before/afterimages from loading) it was nice to finally have a working solution.
And so you know, I had forgotten to refresh the permalinks the first time, but even so ['^blog/$'] didn't work.
Oh, and I didn't even bother wrapping changes to the dev site. You can see it all working on the live site (without 'wpdev')
Thanks again for the help. Next time I'll come the WP-questions before I waste a ton of time.
Monte
Arnav Joy answers:
try this
function html_page_permalink() {
global $wp_rewrite;
if ( !strpos($wp_rewrite->get_page_permastruct(), '.html')){
$wp_rewrite->page_structure = $wp_rewrite->page_structure . '.html';
}
}
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' && !is_front_page()){
return untrailingslashit($string);
}else{
return $string;
}
}
Sirmontegu comments:
Arnav, your answer is simple and makes sense. Unfortunately it doesn't work. Any other ideas?
Hai Bui answers:
Just install this plugin http://wordpress.org/extend/plugins/custom-permalinks/
It will allow you to set a custom permalink for Blog page
Sirmontegu comments:
Hai Bui, I already tried the Custom Permalinks plugin as recommended in the [[LINK href="http://wordpress.org/support/topic/plugin-html-on-pages-excluding-a-page"]]plugin support[[/LINK]].
What happens when I use it kinda weird. It seems to work, but then the post URLs actually display the exact same archive as the blog page.
Hai Bui comments:
Not very clean, but it works: edit custom-permalinks.php (of Custom Permalinks plugin), insert this to line 160:
if (preg_match('/.html/',$request)) return $query;
Dbranes answers:
If you are testing John's solution, I think you will have to flush the rewrite rules:
fx. go to the "Settings > Permalinks" page in wp-admin and press "save" to flush it.
... or mabye you did? ;-)
Budi Kurniawan answers:
why you build a .html suffix for your URL post with plugin?
you could change the permalink with :
/%postname%.html
and you will get it all , and not yet plugin