rosell-dk / webp-express

Wordpress plugin for serving autogenerated WebP images instead of jpeg/png to browsers that supports WebP
GNU General Public License v3.0
221 stars 63 forks source link

Path is outside resolved document root when using Apache Alias directive. #585

Open annpocoyo opened 1 year ago

annpocoyo commented 1 year ago

Problem:

My httpd configuration uses the Alias /blog /opt/homebrew/var/httpd/wordpress directive so I don't break the permissions of the blog. But this causes the plugin to think the path is outside document root.

Relevant part of httpd.conf:

# WordPress
Alias /blog /opt/homebrew/var/httpd/wordpress
<Directory /opt/homebrew/var/httpd/wordpress/>
   Options Indexes FollowSymLinks MultiViews
   AllowOverride All
   <IfModule mod_authz_core.c>
      Require all granted
   </IfModule>
   <IfModule !mod_authz_core.c>
      Order allow,deny
      Allow from all
   </IfModule>
</Directory>
annpocoyo commented 1 year ago

A workaround is to manually set $_SERVER['DOCUMENT_ROOT'] to $_SERVER['CONTEXT_DOCUMENT_ROOT'] which returns the correct document root in this context.

This is best done by setting the auto_prepend_file php configuration directive which automatically loads a php file before any piece of code.

Here's a example:

fix-doc-root.php (Place anywhere):

<?php
$_SERVER['DOCUMENT_ROOT'] = $_SERVER['CONTEXT_DOCUMENT_ROOT'];

Updated part of httpd.conf:

# WordPress
Alias /blog /opt/homebrew/var/httpd/wordpress
<Directory /opt/homebrew/var/httpd/wordpress/>
   # Fix DOCUMENT_ROOT
   php_value auto_prepend_file path/to/fix-doc-root.php

   # Other stuff
   Options Indexes FollowSymLinks MultiViews
   AllowOverride All
   <IfModule mod_authz_core.c>
      Require all granted
   </IfModule>
   <IfModule !mod_authz_core.c>
      Order allow,deny
      Allow from all
   </IfModule>
</Directory>