WordPress Apache .htaccess and Yoast SEO Sitemap not working

Hello All,

My first post, so be gentle :slightly_smiling_face:

I am using the official WordPress image with Apache and can confirm that the htaccess file in the container is being used because I can cause an Server Errror with incorrect code.

All seems well apart from the Yoast SEO plugin - the XML sitemap just will not show up as expected.

I have read this official help page and checked it all…

…but it does not work for me.

Here is an extract of the Docker Compose file which shows the volume mount…

  domain.co.uk-wordpress:
    depends_on:
      - domain.co.uk-wordpress_db
    image: wordpress:latest
    container_name: domain.co.uk-wordpress
    restart: always
    networks:
      nginx-proxy-manager:
        ipv4_address: 172.20.0.30
    volumes:
      - ./data/html:/var/www/html

Here is the content of the .htaccess file…

# Yoast SEO - XML Sitemap Rewrite Fix
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^sitemap_index.xml$ /index.php?sitemap=1 [L]
RewriteRule ^locations.kml$ /index.php?sitemap=wpseo_local_kml [L]
RewriteRule ^geo_sitemap.xml$ /index.php?sitemap=geo [L]
RewriteRule ^([^/]+?)-sitemap([0-9]+)?.xml$ /index.php?sitemap=$1&sitemap_n=$2 [L]
RewriteRule ^([a-z]+)?-?sitemap.xsl$ /index.php?yoast-sitemap-xsl=$1 [L]
</IfModule>
# END Yoast SEO - XML Sitemap Rewrite Fix

# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

…but the SEO Sitemap part is ignored.

Can anyone help to get this working or is this a known limitation of Docker WordPress?

Thanks!

:slight_smile:

Paully

It was years ago when I had to write htaccess rules, so I can’t give you a 100% sure answer, but I don’t think that the SEO section is ignored. I think it is just overridden in the next section.

Try to add more conditions using RewriteCond to the second section so it will not rewrite the path when the requests starts with the filenames from the first section.

If I am right, then this is not related to Docker and it is not even a “limitation” of WordPress. It is just how Rewrite rules work. I remember I had some hard time to figure similar rules out.

Thanks for your quick reply.

What is strange is that this is from the official documentation!

I will try your suggestion and get back to you.

Alas, that does not make it work.

Here is my htaccess file which shows I have combined the 2 batches of rules.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^sitemap_index.xml$ /index.php?sitemap=1 [L]
RewriteRule ^locations.kml$ /index.php?sitemap=wpseo_local_kml [L]
RewriteRule ^geo_sitemap.xml$ /index.php?sitemap=geo [L]
RewriteRule ^([^/]+?)-sitemap([0-9]+)?.xml$ /index.php?sitemap=$1&sitemap_n=$2 [L]
RewriteRule ^([a-z]+)?-?sitemap.xsl$ /index.php?yoast-sitemap-xsl=$1 [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Oh, that’s just been changed back by WordPress automatically, which is why Yoast said to add it before as a separate code block.

I think I need to ask Yoast themselves for help on this.

Either that, or change to not use Docker but MariaDB and NginX instead - on which I know XML sitemaps work…

I think it would be good for the Docker community to fix this problem though… I am sure plenty of other companies use Docker WordPress in their production web sites and need the SEO XML Sitemaps working!

Regards,

Paully

Okay, I think I didn’t see the whole picture here and I also read your post too quickly. Now I understand “Yoast SEO plugin” is Wordpress plugin which is what you indeed wrote at the beginning. Since you shared a link I went there and I saw the additional part in the .htaccess file thinking that you added it manually. If you did, I think you should not have done it. If you just activated the SEO plugin which regenerated the .htaccess file, that should have worked. I installed the latest Wordpress from the official Docker image, then I activated “permalink” in the settings (I changed from “plain” to “post name”. After that I installed Yoast SEO plugin and tried to load http://localhost:8080/sitemap_index.xml. It worked.

I even tried to change permalinks back to “Plain” and the sitemap still worked.

So it looks like I was wrong. The htaccess rules are good so I don’t know why it didn’t work for you.

Yes. It is also in the comments but I missed that part too…

I just realized The description of the official Docker image for Wordpress mentions that the image is maintained by the “Docker Community”, but I don’t know which part of the community and whether the developers are here on the “Community Forum”. Maybe an older member could tell that.

So if you think the image needs some improvement, the best place to ask for it may be the GitHub repository in a new issue.
My personal opinion is that WordPress does not make it easier to create really a good Docker image for the CMS. For example: because it writes the .htaccess, it needs to have permission to write the document root, which is not a good idea. Containerizing an application is not always just putting it inside a container

However, in this case, I think we should find out why this Yoast SEO plugin works for me, but not for you. The only difference I can see between our coniguration is that I did not mounted the document root. I just run the example from the Docker image description

version: '3.1'

services:

  wordpress:
    image: wordpress
    restart: always
    ports:
      - 8080:80
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: exampleuser
      WORDPRESS_DB_PASSWORD: examplepass
      WORDPRESS_DB_NAME: exampledb
    volumes:
      - wordpress:/var/www/html

  db:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_DATABASE: exampledb
      MYSQL_USER: exampleuser
      MYSQL_PASSWORD: examplepass
      MYSQL_RANDOM_ROOT_PASSWORD: '1'
    volumes:
      - db:/var/lib/mysql

volumes:
  wordpress:
  db:

Wow, what a reply - thank you so much!

I shall spin up a fresh WP image using your compose file, follow your instructions exactly and see if THAT works for me as well.

OK, now that is VERY interesting… following your instructions exactly it worked for me to!

Running out-of-the-box Docker Wordpress, change Permalinks, install and activate Yoast SEO plugin.

:8080/sitemap_index.xml worked first time.

Now, to run it through Nginx Proxy Manager on port 80 as a proxied host…

…which is does!

/sitemap.xml worked first time.

The only difference I can see with the live ‘problem’ site is the Permalink structure - which is on /%category%/%postname%/ instead of just /%postname%/

…and sure enough, if I change the test site I have just created to the same Permalink structure of /%category%/%postname%/ then it gives the error of 404.

Ah ha - we have found the cause of the error!

How can I keep the /%category%/%postname%/ permalink structure and use Yoast XML Sitemaps?

Can you try that same change on your test site please?

Thanks,

Paully

I tried it with a custom permalink structure using category and postname. The result is the same. It works. I also tried to access sitemap.xml because you mentioned. I thought it was mistake and you meant sitemap_index.xml but it redirected to sitemap_index.xml even in a different browser. Now the there is an other differnce between our configs. I don’t use nginx proxy