S3-Uploads icon indicating copy to clipboard operation
S3-Uploads copied to clipboard

URL rewrite not working for specific custom uploads directory

Open mxkyb opened this issue 6 years ago • 4 comments

I had to work with a wordpress installation, where the previous admin changed the uploads directory into /files, where / is the wordpress root directory. So it was moved outside of the wp-content directory. In this case the plugins url rewrite doesn't work.

I guess this is due to a filter in S3-Uplaods/inc/class-s3-uploads.php

 	public function filter_upload_dir( $dirs ) {

		$this->original_upload_dir = $dirs;

		$dirs['path']    = str_replace( WP_CONTENT_DIR, 's3://' . $this->bucket, $dirs['path'] );
		$dirs['basedir'] = str_replace( WP_CONTENT_DIR, 's3://' . $this->bucket, $dirs['basedir'] );

		...

Obviously, when the path and the basedir both do not contain the wp-content directory, the search and replace doesn't match. The results are 404 links for every media file, it looks something like this:

http://serverdomain.tld/host/directory/to/wordpress/files/image.jpg, compared to the correct url http://serverdomain.tld/files/image.jpg or the adjusted bucket url.

I guess the easiest fix would be, to not move the uploads folder outside of the wp-content. But since it is very hard to move an existing media library back into the wp-content and undo the mistake of the past, it maybe would be better to check for this case and adjust the replacement calls.

mxkyb avatar Oct 21 '18 23:10 mxkyb

This isn't a particularly common use case (certainly, we've not run into it), so I'm not sure we want to account for it inside S3 Uploads. This is essentially the same as having a conflicting plugin that also changes the upload location.

Is it possible to handle this with an mu-plugin or similar to manually filter those URLs?

rmccue avatar Oct 23 '18 02:10 rmccue

I'm also running into this issue

vichang avatar May 14 '19 07:05 vichang

I am also facing this issue. I am currently serving all my uploads from a folder called static and used as a subdomain so all the upload URLs are like this:

static.domain.com/uploads/year/month/file.jpeg

If I set S3_UPLOADS_DISABLE_REPLACE_UPLOAD_URL to false, then all the uploads URL appear like this:

domain.com/home/user/public_html/static/uploads/year/month/file.jpg

Of course every image returns a 404.

eperezf avatar Sep 05 '20 05:09 eperezf

Managed to fix it. in S3-Uploads/inc/class-s3-uploads.php, replace

$dirs['path']    = str_replace( WP_CONTENT_DIR, 's3://' . $this->bucket, $dirs['path'] );

$dirs['basedir'] = str_replace( WP_CONTENT_DIR, 's3://' . $this->bucket, $dirs['basedir'] );

with

$dirs['path']    = str_replace( 'path/of/your/files/folder', 's3://' . $this->bucket, $dirs['path'] );

$dirs['basedir'] = str_replace( 'path/of/your/files/folder', 's3://' . $this->bucket, $dirs['basedir'] );

so in my case, I was uploading to /home/user/public_html/static/uploads so my fix was like this:

$dirs['path']    = str_replace( '/home/user/public_html/static/uploads', 's3://' . $this->bucket, $dirs['path'] );

$dirs['basedir'] = str_replace( '/home/user/public_html/static/uploads', 's3://' . $this->bucket, $dirs['basedir'] );

Maybe defining a configuration setting for this special case might be useful.

eperezf avatar Sep 05 '20 06:09 eperezf