Set Post Name Permalinks in WordPress on Heroku
-
So here’s the issue. Heroku, as you know, restarts its ‘dynos’ periodically which deletes temp files and basically restarts your app. The dynos also restart when you push a new version of your app.
When the dynos restart, all of my permalinks (besides the home page) lead to 404s, so I have to go to Settings->Permalinks and simply click the “Save Changes” button. Then all of my permalinks are set correctly. It’s strange, especially considering that the “Post Name” radio button is already checked.
The issue is that I never know when the dynos will restart on their own, so I need to programmatically set the permalinks to “Post Name” whenever this happens.
Here are some approaches that I have attempted without success:
In functions.php:
// set permalinks on heroku
function reset_permalinks() {
global $wp_rewrite;
$wp_rewrite->set_permalink_structure( ‘/%postname%/’ );
$wp_rewrite->flush_rules();
}
add_action( ‘init’, ‘reset_permalinks’ );
In functions.php simply:global $wp_rewrite;
$wp_rewrite->set_permalink_structure( ‘/%postname%/’ );
In .htaccess:# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /sitename/
RewriteRule ^index\.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /sitename/index.php [L]
</IfModule># END WordPress
None of this seems to work. It’s extremely frustrating! My site is also on https, and I’m using some plugins to redirect to https. I don’t think this could be related though, as going to the http version of links results in the same Internal Service Error.Any help would be greatly appreciated. I’m thinking I may just have to find all of the logic that occurs when I click “Save Changes”, put that in a php file, and add it as a heroku deployhook or wordpress init hook.
Edit: Before and after clicking “Save Changes” my var_dump($wp_rewrite) is the same, containing [“permalink_structure”]=> string(12) “/%postname%/”
Edit 2: I’ve tried so many things. I added a deployhook which I watch for on my home page by checking for the ‘app’ POST variable, and I copied all of the logic that I can think of from the options-permalinks.php
if (isset($_GET[‘pleaserewrite’]) || isset($_POST[‘app’])) {
global $wp_rewrite;
$permalink_structure = “/%postname%/”;
$prefix = $blog_prefix = ”;
if ( ! empty( $permalink_structure ) ) {
$permalink_structure = preg_replace( ‘#/+#’, ‘/’, ‘/’ . str_replace( ‘#’, ”, $permalink_structure ) );
if ( $prefix && $blog_prefix )
$permalink_structure = $prefix . preg_replace( ‘#^/?index\.php#’, ”, $permalink_structure );
else
$permalink_structure = $blog_prefix . $permalink_structure;
}$wp_rewrite->set_permalink_structure( $permalink_structure );
$wp_rewrite->flush_rules();
}
Could it have something to do with permissions? What could possibly be included on the options-permalinks.php that isn’t included here?
- The topic ‘Set Post Name Permalinks in WordPress on Heroku’ is closed to new replies.