URL /子域重写(htaccess的) [英] URL/Subdomain rewrites (htaccess)

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

问题描述

说我有以下文件:

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

我想为它

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

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

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

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

Like for example, I make a subdomain s1.mysite.com and then on that subdomain, I add a htaccess rule to point any files, to pull it from http://www.mysite.com/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.com 设置cookie还将与要求 s1.mysite发送。 COM S1。是一个子域 mysite.com 。你需要使用 WWW。(或者你选择的任何其他子域)以分离cookie的空间。

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

For simplicity, I'm assuming that all domains point to the same directory, so that www.mysite.com/something = mysite.com/something = s1.mysite.com/something = blub.mysite.com/something. This makes things simpler if you really need store the images physically in "www.mysite.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\.mysite\.com
# Prevent an endless loop frm ever happening
RewriteCond %{REQUEST_URI} !^/images
RewriteRule (.+) /images/$1 [L]

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

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

# Place any additional rewrites below.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆