URL/子域重写 (htaccess)

编程入门 行业动态 更新时间:2024-10-27 15:15:30
本文介绍了URL/子域重写 (htaccess)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设我有以下文件:

www.example/images/folder/image.jpg

我想提供它

s1.example/folder/image.jpg

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

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

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

Like for example, I make a subdomain s1.example and then on that subdomain, I add a htaccess rule to point any files, to pull it from www.example/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 设置的 cookie 也将与对 s1.example 的请求一起发送为s1." 是 example 的子域.您需要使用 www.(或您选择的任何其他子域)来分隔 cookie 空间.

One thing to keep in mind, too, is that cookies set for example will also be sent with requests to s1.example as "s1." is a subdomain to example. 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/something = example/something = s1.example/something = blub.example/something.如果您确实需要将图像物理存储在www.example/images"中,这会使事情变得更简单.

For simplicity, I'm assuming that all domains point to the same directory, so that www.example/something = example/something = s1.example/something = blub.example/something. This makes things simpler if you really need store the images physically in "www.example/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 # Prevent an endless loop from ever happening RewriteCond %{REQUEST_URI} !^/images RewriteRule (.+) /images/$1 [L] # Redirect s1.example/ to the main page (in case a user tries it) RewriteCond %{HTTP_HOST} ^s1.example RewriteRule ^$ www.example/ [R=301,L] # Redirect all requests with other subdomains, or without a subdomain to www. # Eg, blub.example/something -> www.example/something # example/something -> www.example/something RewriteCond %{HTTP_HOST} !^www.example RewriteCond %{HTTP_HOST} !^s1.example RewriteRule ^(.*)$ www.example/$1 [R=301,L] # Place any additional rewrites below.

更多推荐

URL/子域重写 (htaccess)

本文发布于:2023-10-31 17:29:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546774.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重写   URL   htaccess

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!