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
225 stars 64 forks source link

Windows generated .htaccess files contain unexpected backslashes in file names #512

Closed bobbingwide closed 2 years ago

bobbingwide commented 3 years ago

In my Windows development environment the automatic redirection logic wasn't working as expected. Looking at the generated .htaccess files I noted that the file names in the Redirect section contained a mixture of forward slashes and backslashes.

 # Redirect to existing converted image in cache-dir (if browser supports webp)
  RewriteCond %{HTTP_ACCEPT} image/webp
  RewriteCond %{REQUEST_FILENAME} -f
  RewriteCond %{REQUEST_FILENAME} (?i)(C:/apache/htdocs\cwiccer/wp-content/uploads/)(.*)(\.jpe?g|\.png)$
  RewriteCond C:/apache/htdocs\cwiccer/wp-content/webp-express/webp-images/uploads/%2%3.webp -f
  RewriteRule (?i)(.*)(\.jpe?g|\.png)$ /cwiccer/wp-content/webp-express/webp-images/uploads/%2%3\.webp [T=image/webp,E=EXISTING:1,E=ADDVARY:1,L]

I believe this would prevent the RewriteRule from working. Changing the backslashes to forward slashes resolved the issue.

Explanation

In this installation ABSPATH is C:\apache\htdocs\cwiccer/ Additionally, I've installed the plugin using a symlink to C:\apache\htdocs\wordpress\wp-content\plugins\webp-express In the logic that generates the .htaccess file(s) it would appear that not all backslashes are being converted to forward slashes.

Proposed fix

In lib/classes/PathHelper.php change the canonicalize method to convert backslashes to forward slashes.

 public static function canonicalize($path)  {
      $path = str_replace( "\\", '/', $path);
      $parts = explode('/', $path);
rosell-dk commented 2 years ago

Thank you for debugging this!

rosell-dk commented 2 years ago

@bobbingwide: I should mention that the new 0.22.0 release overwrites the .htaccess files (due to another fix). So your manual fix will be overwritten

rosell-dk commented 2 years ago

I have fixed and released as 0.22.1.