0

With Apache, how to make a URL (lets say an Image Path) to be actually coming from another location. For example, i have a html page with an URL like:

apple.html, with the original codes inside:

<img src="http://www.myoldsite.com/assets/image/apple.jpg" />
<img src="http://www.myoldsite.com/assets/image/orange.jpg" />

But actually i want this path to be re-written to:

<img src="http://www.mynewsite.com/image/apple.jpg" />
<img src="http://www.mynewsite.com/image/orange.jpg" />

So when i access the apple.html page, the image should be coming from new location, without needed to go and change the actual codes inside the files each.

But actually i'm totally weak in .htaccess rules. When i used like following, it is showing error:

RewriteEngine On
RewriteRule http://www.mynewsite.com/image/(.*) /assets/image/$1 [QSA, L]

I am having thousands of pages like this but I am totally stuck with .htaccess rules.
What am i missing please? Please help.

4

2 回答 2

0

我假设两个域都指向同一个 apache 实例?

您似乎已经切换了RewriteRule. 语法是:

RewriteRule UrlsMatchingThis ChangeToThis [params]

所以在你的情况下,我会做这样的事情:

RewriteRule ^/assets/image/(.*) http://www.mynewsite.com/image/$1  [R,NC,L]

这匹配 tld 之后的部分是 /assets/image/(anything) 的所有请求。我使用的标志是

R redirect
NC Ignore case
L Last rewrite rule for this request

您可能希望向这样的条件添加条件RewriteRule

RewriteCond %{SERVER_NAME} ^(www\.)?myoldsite\.com$                [NC]
RewriteRule ^/assets/image/(.*) http://www.mynewsite.com/image/$1  [R,NC,L]

如果域是 myoldsite.com,这只会重写您的请求,无论是否带有www.

请参阅 http://httpd.apache.org/docs/current/mod/mod_rewrite.html以获取重写模块的文档。

于 2013-06-19T08:35:20.377 回答
0

通过启用 mod_rewrite 和 .htaccess httpd.conf,然后将此代码放在您.htaccessDOCUMENT_ROOT目录下:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteRule ^(image/.+)$ /assets/$1 [NC,L]
于 2013-06-19T11:32:21.543 回答