简体   繁体   English

htaccess重写规则,用于将文件夹中的所有图像复制到另一个域

[英]htaccess rewrite rule for all images in a folder to another domain

I have a live site (livesite.com) and a dev site (devsite.com). 我有一个实时站点(livesite.com)和一个开发站点(devsite.com)。 The images directory is so huge that I am not able to copy it from livesite.com to devsite.com. images目录是如此之大,以至于我无法将其从livesite.com复制到devsite.com。 Therefore, I want to add a rewrite rule for devsite.com to fetch the image from livesite.com (the directory structure on both sites are identical - its just the domain that's different) 因此,我想为devsite.com添加重写规则,以从livesite.com获取图像(两个站点上的目录结构相同-只是域不同)

It's a wordpress site so the images are located in this structure for each month; 这是一个wordpress网站,因此每个月图像都位于此结构中;

/wp-content/uploads/2015/01/image.jpg
/wp-content/uploads/2015/02/image.jpg
etc..

I need to add a rewrite rule just for everything in the 2015 directory. 我需要为2015目录中的所有内容添加重写规则。

So far, this is what I have in the htaccess file in the root of devsite.com; 到目前为止,这就是我在devsite.com根目录下的htaccess文件中拥有的内容; It's targetting a specific sub-directory in the 2015 folder and I can't even get this to work - the images are still trying to be retrieved from the devsite.com domain rather than livesite.com 它的目标是2015文件夹中的特定子目录,我什至无法使它工作-图像仍在尝试从devsite.com域而不是livesite.com检索

Options +FollowSymLinks
RewriteEngine On
RewriteRule /wp-content/uploads/2015/01/(.*)$ http://livesite.com/wp-content/uploads/2015/01/$1 [R=302,NC,L]

I suspect I have made a bumbling error here - any help appreciated! 我怀疑我在这里犯了一个令人毛骨悚然的错误-感谢您提供任何帮助!

The problem is with the pattern, which starts with a slash, see RewriteRule 问题在于模式,该模式以斜杠开头,请参见RewriteRule

Per-directory Rewrites 每目录重写
- The removed prefix always ends with a slash, meaning the matching occurs against a string which never has a leading slash. -删除的前缀始终以斜杠结尾,这意味着匹配不会出现任何前导斜杠的字符串。 Therefore, a Pattern with ^/ never matches in per-directory context. 因此,带有^ /的模式在每个目录上下文中都不会匹配。

So, in your case it should be 因此,在您的情况下

RewriteRule ^wp-content/uploads/2015/01/(.*)$ http://livesite.com/wp-content/uploads/2015/01/$1 [R=302,NC,L]

Although, since you want to cover the whole 2015 subdirectory, you can simplify this to 虽然,由于您想覆盖整个2015子目录,所以可以将其简化为

RewriteRule ^wp-content/uploads/2015/(.*)$ http://livesite.com/wp-content/uploads/2015/$1 [R,NC,L]

or even 甚至

RewriteRule ^wp-content/uploads/2015/(.*)$ http://livesite.com%{REQUEST_URI} [R,NC,L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM