本文介绍了URL/子域重写 (htaccess)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下文件:

http://www.example.com/images/folder/image.jpg

我想提供它

http://s1.example.com/folder/image.jpg

如何进行 htaccess 重写以将其指向它?

How can I do a htaccess rewrite to point it to it?

例如,我创建了一个子域 s1.example.com,然后在该子域上,我添加了一个 htaccess 规则来指向任何文件,从 http://www.example.com/images/

Like for example, I make a subdomain s1.example.com and then on that subdomain, I add a htaccess rule to point any files, to pull it from http://www.example.com/images/

以这种方式提供文件是否相当于提供来自无 cookie 域的内容?

Does serving files this way act as serving content from a cookieless domain?

推荐答案

首先让我谈谈 cookieless 域的概念.通常,当通过 http 请求任何内容时,任何相关的 cookie 都会与请求一起发送.Cookie 取决于它们来自哪个域.使用无 cookie 域的想法是将没有 cookie 的静态内容(如图像)重新定位到一个单独的域,以便不会随该请求发送 cookie.这会减少少量流量.

First let me talk a bit about the concept of cookieless domains. Normally, when requesting anything over http, any relevant cookies are sent with the request. Cookies, are dependent on which domain they come from. The idea of using a cookieless domain is that you relocate static content that doesn't cookies, like images, to a separate domain so that no cookies will be sent with that request. This cuts out a small amount of traffic.

您从这样做中获得多少取决于页面的类型.您拥有的图像越多,您获得的就越多.如果您的网站加载了大量小图像,例如头像或图像缩略图,您可能会收获很多.相反,如果您的网站不使用任何 cookie,您将一无所获.如果您的页面仅使用少量图像(无论如何都会在页面加载之间进行缓存),则完全有可能您的页面加载速度不会明显加快.

How much you gain from doing this depends on the type of page. The more images you have, the more you gain. If your site loads a big bunch of small images, such as avatars or image thumbnails, you might have a lot to gain. On the contrary, if your site doesn't use any cookies, you have nothing to gain. It's entirely possible that your page won't load noticeably faster, if it only uses a small amount of images, which will be cached between page loads anyway.

还要记住的一件事是,为 example.com 设置的 cookie 也将与对 s1.example.com 的请求一起发送为s1." 是 example.com 的子域.您需要使用 www.(或您选择的任何其他子域)来分隔 cookie 空间.

One thing to keep in mind, too, is that cookies set for example.com will also be sent with requests to s1.example.com as "s1." is a subdomain to example.com. You need to use www. (or any other subdomain of your choice) in order to separate the cookie spaces.

其次,如果您认为无 cookie 域确实值得尝试,那么让我们谈谈实现.

Secondly, if you decide that a cookieless domain is actually something worth trying, let's talk about the implementation.

Shikhar 的解决方案糟糕!虽然该解决方案表面上看起来可行,但实际上违背了使用无 cookie 域的目的.对于每个图像,首先尝试 s1. url.s1. URL 然后重定向到 www. 域,触发第二个 http 请求.无论你怎么看,这都是一种损失.您需要的是重写,它在网络服务器内部更改 URL,浏览器甚至没有意识到.

Shikhar's solution is bad! While the solution appears to work on the surface, it actually defeats the purpose of using a cookieless domain. For every image, first the s1. url is tried. The s1. URL then makes a redirect to the www. domain which triggers a second http request. This is a loss, no matter how you look at it. What you need is a rewrite, which changes the URL internally on the web server, without the browser even realizing.

为简单起见,我假设所有域都指向同一个目录,因此 www.example.com/something = example.com/something = s1.example.com/something = blub.example.com/something.如果您确实需要将图像物理存储在www.example.com/images"中,这会使事情变得更简单.

For simplicity, I'm assuming that all domains point to the same directory, so that www.example.com/something = example.com/something = s1.example.com/something = blub.example.com/something. This makes things simpler if you really need store the images physically in "www.example.com/images".

我推荐一个看起来像这样的 .htaccess:

I'd recommend a .htaccess that looks a little something like this:

# Turn on rewrites
RewriteEngine On

# Rewrite all requests for images from s1, so they are fetched from the right place
RewriteCond %{HTTP_HOST} ^s1.example.com
# Prevent an endless loop from ever happening
RewriteCond %{REQUEST_URI} !^/images
RewriteRule (.+) /images/$1 [L]

# Redirect http://s1.example.com/ to the main page (in case a user tries it)
RewriteCond %{HTTP_HOST} ^s1.example.com
RewriteRule ^$ http://www.example.com/ [R=301,L]

# Redirect all requests with other subdomains, or without a subdomain to www.
# Eg, blub.example.com/something -> www.example.com/something
#     example.com/something      -> www.example.com/something
RewriteCond %{HTTP_HOST} !^www.example.com
RewriteCond %{HTTP_HOST} !^s1.example.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

# Place any additional rewrites below.

这篇关于URL/子域重写 (htaccess)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 12:30