Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Pretty upload permalinks in multisite WordPress

  • REFUNDED

I need a permalink rewrite that will make WordPress uploads in a multi-site environment appear pretty.

This is how links look now:
http://url.com/wp-content/blogs.dir/21/files/2011/06/650026_x640_21.jpg
-or-
http://subdomain.url.com/wp-content/blogs.dir/21/files/2011/06/650026_x640_21.jpg

This is how they should look:
http://url.com/media/2011/06/650026_x640_21.jpg
-or-
http://subdomain.url.com/media/2011/06/650026_x640_21.jpg

Edit:
I'm looking for a solution that does not have the blogid hard-coded into the rule.

Answers (2)

2011-07-04

John Cotton answers:

It's not clear what the rules will be from the one example above, but sticking this line into your .htaccess file should work for the case above.

RewriteRule ^media/(\d\d\d\d)/(\d\d)/(.+) wp-content/blogs.dir/21/files/$1/$2/$3 [NC]


Matt Taylor comments:

John,

I edited my question to reflect this, but if I remove the blog id from the permalink you wrote should it still work?

Thanks!
~Matt


John Cotton comments:

No - the blog id is being used as a directory name.

How can you tell from the shorten version which blog the image comes from? Is the -21 on the end of the image name significant? Is that the blog id?


Matt Taylor comments:

From my understanding the 21 in the blogs.dir/21/files/*** is the site-id


Matt Taylor comments:

As for which site did the images come from the most logical assumption is the siteurl.


Matt Taylor comments:

As for which site did the images come from the most logical assumption is the siteurl.


John Cotton comments:

Sure, but do we know that from the media/ link? There needs to be a rule so that it be put in the rewrite.

Is it coincidence that the file name has _21.jpg on the end? If not, then assuming the _21 on the end of file is the blog id, this would work:

RewriteRule ^media/(\d\d\d\d)/(\d\d)/(.+)_([0-9]+).(gif|jpg|jpeg|png)$ wp-content/blogs.dir/$4/files/$1/$2/$3_$4.$5 [NC]


Matt Taylor comments:

Sadly it is a coincidence. :)

Its not a big deal to me to have the blogid in the permalink, I just dont want to have (for example) 30 lines for it in my htaccess.

Would having the new url be <em>/media/blogid/filename.whatever</em> work?


John Cotton comments:

Ah - OK I get it now.

Have a look at this article:

[[LINK href="http://alvistor.com/web-wizard/wordpress-web-wizard/wordpress-mu-media-upload-problem-solved/"]]http://alvistor.com/web-wizard/wordpress-web-wizard/wordpress-mu-media-upload-problem-solved/[[/LINK]]


Matt Taylor comments:

That actually broke the display of every thumbnail. Perhaps this cannot be done. :/

2011-07-05

Ipstenu answers:

They should look like like this:
http://url.com/files/2011/06/650026_x640_21.jpg
-or-
http://subdomain.url.com/files/2011/06/650026_x640_21.jpg

That's the MultiSite default setup. Did you change something to make them 'media' and, if so, did you also change your .htaccess so where /files points to the ms-files.php file, it now redirects media as well?

It'd be like this I believe...

# uploaded files
RewriteRule ^media/(.+) wp-includes/ms-files.php?file=$1 [L]


(fixed code for subdomain!)


Matt Taylor comments:

Hi Ipstenu, the permalinks don't show /media/ right now. I'm looking for (at a minimum) a permalink code to re-write /wp-content/blogs.dir/ as /media/.


Ipstenu comments:

Matt, do they show as FILES though?

On my sites, I get ipstenu.org/files/2011/07/05/file.jpg for images, which redirects. That's <em>default</em> behavior, which is what I'm driving at :) If that works, we can redirect it, but I want to know what we're starting with ;)

Your .htaccess SHOULD have a line in it like this (for a subdomain):
RewriteRule ^files/(.+) wp-includes/ms-files.php?file=$1 [L]

Do you have that?


Matt Taylor comments:

Yes, I have tried it with that and RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]


Ipstenu comments:

You should only need the second one I posted. The one with the ([_0-9a-zA-Z-]+/)? in front is for subfolders. And with that, going to http://subdomain.url.com/files/2011/06/650026_x640_21.jpg DOES NOT work? Cause if not, that's where your problems will be.

If you want media to redirect like files, you could either duplicate what files does, or get files working and then just say RewriteRule ^media/(.+) wp-includes/ms-files.php?file=$1 [L]

Obviously, though, if the default set up isn't working, something's wrong on your server with .htaccess. I'd start looking at httpd.conf and make sure AllowOverride is set to All.