URL /子域重写(htaccess的)

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

说我有以下文件:

www.mysite/images/folder/image.jpg

我想为它

s1.mysite/folder/image.jpg

如何做htaccess的重写它指向它?

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

例如像,我做一个子域s1.mysite,然后在该子域名,我添加了一个htaccess的规则,使其指向任何文件,从的 www.mysite/images/

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

是否提供文件服务这样的行为从一个Cookie的域名服务的内容?

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

推荐答案

第一让我谈谈无Cookie域的概念。通常情况下,要求任何over HTTP时,任何相关的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.

有一点要记住,也就是对 mysite 设置cookie还将与要求 s1.mysite发送。 COM 为 S1。是一个子域 mysite 。你需要使用 WWW。(或者你选择的任何其他子域)以分离cookie的空间。

One thing to keep in mind, too, is that cookies set for mysite will also be sent with requests to s1.mysite as "s1." is a subdomain to mysite. 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中的Web服务器上,没有浏览器,甚至实现了。

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.mysite/something = mysite的。 COM /什么 = s1.mysite/something = blub.mysite/something 。这使事情变得更简单,如果你真的需要在物理存储图像 www.mysite/images

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

更多推荐

URL /子域重写(htaccess的)

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

发布评论

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

>www.elefans.com

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