• alexthegoodman

    (@alexthegoodman)


    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?

Viewing 4 replies - 1 through 4 (of 4 total)
  • I would love to see a solution to this.

    The last thing I tried was this:

    # ----------------------
    # BEGIN WordPress
    
    RewriteCond %{HTTP_HOST} ==localhost
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /site-name/
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /site-name/index.php [L]
    </IfModule>
    
    RewriteCond %{HTTP_HOST} !=localhost
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    
    # END WordPress

    Didn’t seem to work.

    Any updates on this? I would also love to know if someone figured it out.

    For me, I’m using a reverse proxy, so the wordpress url and the site url in the settings -> general section are different. Just saving from the settings -> permalinks doesn’t work; I have to save the site url to match the wordpress url, save, then retype in my site url and save. Only then do the permalinks work.

    Maybe the solution is to not run wordpress on heroku….

    Managed to solve the issue.

    I have a main Rails app running on Heroku at https://www.mysite.com
    I installed WordPress on Heroku and have it running on a different url.
    I have a reverse-proxy set up on my Rails app, such that my blog looks like it is running as a subfolder: https://www.mysite.com/BLOG_AS_SUBFOLDER

    Here is my .htaccess

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /BLOG_AS_SUBFOLDER/
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    
    <IfModule mod_headers.c>
    <FilesMatch "\.(svg|ttf|otf|eot|woff|woff2)$">
        Header set Access-Control-Allow-Origin "*"
    </FilesMatch>
    </IfModule>

    The only thing I had to change compared to every other answer on the internet was to modify the following line:

    RewriteRule . BLOG_AS_SUBFOLDER/index.php [L]
    to
    RewriteRule . /index.php [L]

    Rhyun’s solution will not work once heroku restarts (which is whenever you deploy, or randomly once a day, if you use it as a free service)

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Set Post Name Permalinks in WordPress on Heroku’ is closed to new replies.