#StackBounty: #permalinks #filters #url-rewriting Remove the Parent category from the permalink but leave the child category

Bounty: 50

I had a site with a url structure like

example.com/news/story-1
example.com/news/local/story-2
example.com/food/tacos/story-3

Now it’s been moves to a multisite, with /news/ and /food/ as the site subfolders.

Within those sub-sites, the top level category is News or Food, respectively. So if the permalink is set to /%category%/%postname%/, the permalink come out as

example.com/news/news/local/story-title
example.com/food/food/tacos/story-title

In order to remove the category that’s a duplicate of the site subfolder, I added this to functions.php:

add_filter( 'post_link', 'remove_parent_category', 10, 3 );
    function remove_parent_category( $permalink, $post, $leavename ) {
        $permalink_array = explode("/", $permalink);
        $clean_permalink = array_unique($permalink_array);
        $new_permalink = implode("/", $clean_permalink);
        return $new_permalink;
    }  

This works, giving me back

example.com/news/local/story-2
example.com/food/tacos/story-3

and the ability to navigate to

example.com/news/
example.com/news/local
etc.

However posts that are in the top level category now 404 (which I suppose is to be expected) because their url is now

example.com/news/story-1

(which is what I want).

From what I’m gathering I think I need to use either pre_post_link or a rewrite rule to treat those top level posts as if my permalink structure is just %postname%/, but that’s where I’m stuck.


Get this bounty!!!

#StackBounty: #permalinks #404-error #https 404 on pretty permalinks when serving via HTTPS

Bounty: 100

I deployed my wordpress site in Google Cloud Compute Engine. To enable SSL I followed the the steps outlined here https://www.onepagezen.com/free-ssl-certificate-wordpress-google-cloud-click-to-deploy

  1. Install Certbot Client
  2. Generate Certificates
  3. Configure the Certificates
  4. Enable HTTPS Redirect
  5. Restart Apache Server
  6. Update WordPress URLs
  7. Configure SSL Auto-Renewal

Edit /etc/apache2/sites-available/default-ssl.conf

<IfModule mod_ssl.c>
    <VirtualHost _default_:443>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        <Directory /var/www/html>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            Allow from all
            Require all granted
        </Directory> 
    ...

and /etc/apache2/sites-available/wordpress.conf

<VirtualHost *:80>

  ServerAdmin webmaster@localhost
  DocumentRoot /var/www/html
  ServerName mydomain.com
  ServerAlias mydomain.com
  Redirect permanent / https..mydomain.com

  <Directory />
    Options FollowSymLinks
    AllowOverride None
  </Directory>

  <Directory /var/www/html/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    Allow from all
  </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
  <Directory "/usr/lib/cgi-bin">
    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all
  </Directory>

  ErrorLog ${APACHE_LOG_DIR}/error.log
  # Possible values include: debug, info, notice, warn, error, crit,
  # alert, emerg.
  LogLevel warn
  CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

After setting everything up, I run:

sudo a2ensite default-ssl
sudo a2enmod ssl
sudo service apache2 restart

SSL works fine for the backend and the landing page. However for all other pages I get a 404. When I switch form custom permalinks to simple permalinks the pages are accessible again. Any ideas what might be causing the problem?


Get this bounty!!!