子域缠结(需要编织者)

发布于 2025-01-04 23:08:20 字数 656 浏览 4 评论 0 原文

我似乎已经因为一系列网站子域的计划而将自己逼入了困境。我想知道我的计划是否可以挽救,或者我真的搬起石头砸自己的脚,需要完全重写大量的东西吗?问题是:

我计划了一系列子域站点,所有这些站点都处理主题的变体。为了便于说明,我们假设我有网站 www.colour.com(这是文件驻留在 public_html 中的“中心”),然后添加子域 red.colour.com(在 public_html/red 中)、green.colour.com(在 public_html/green 中)和 blue.colour.com(在 public_html/blue 中)。好的,到目前为止一切都很好。

问题是这些网站都共享很多共同资源 - 样式表、Javascript、图像等。我不想复制这些资源,因为这是浪费空间,但更重要的是我不想冒险开发不同版本的文件,并且它们并不全部保持同步。所以我做了我认为明智的事情,并将所有这些存储在“中心”(在 public_html/css、public_html/js 等中)。

当我的网站即将上线时,我发现一旦我将 public_html/red 定义为 red.colour.com,它就无法再“看到”位于更高一级的任何支持文件(在 ../ ),因此外观和功能分解成一个完整的屏幕混乱。

除了重大重写之外,有没有任何人能想到的摆脱这种混乱的方法呢?

提前致谢!

坦率。

I seem to have painted myself into a corner with my plans for a series of website sub-domains. I wonder if my plans can be rescued or have I truly shot myself in the foot and would need to totally re-write a ton of stuff? Here's the problem:

I planned a series of sub-domain sites all dealing with variations on a theme. For illustrative purposes let's pretend I have the site www.colour.com (this is the 'hub' whose files reside in public_html) and then I add subdomains red.colour.com (in public_html/red), green.colour.com (in public_html/green) and blue.colour.com (in public_html/blue). Ok all good up to this point.

The thing is that these sites all share a lot of resources in common - style sheets, Javascripts, images etc. These are resources I don't want to replicate because it's a waste of space, but more importantly I don't want to risk developing different versions of files and them not all keeping in step with each other. So I did what I thought was the sane thing and I store all these at the 'hub' (in public_html/css, public_html/js etc).

What I discovered when my site went nearly live was that as soon as I defined e.g. public_html/red as red.colour.com, it could no longer 'see' any of it's supporting files that were located one level higher (in ../) and so the appearence and functionality broke down into a complete screen mess.

Short of a major re-write, is there any way out of this mess that anyone can think of?

Thanks in advance!

Frank.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

囍笑 2025-01-11 23:08:20

我破解了!
它需要一品脱的血液、两加仑的汗水和三大瓶的眼泪,但我终于破解了它。
正如我开始怀疑的那样,秘密是使用 mod.rewrite 。您确实不能寻址根以上的区域,但还有其他方法来引用这些位置,特别是您可以使用此处的重写条件来引用“不存在”的文件或目录:

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

例如,当您想要引用比当前上下文更高级别的样式表目录,您会引用 ../css,但是在将当前上下文放入其自己的子域后,../css 不再存在。这意味着您可以与上述一个或两个重写条件进行匹配。现在,在我的示例中,这只是通过原始域 www.colour.com/css 将客户端指向(例如)css 目录的情况。在这里,为了完整起见,并帮助任何其他可怜的灵魂不要因此自杀(我必须学习正则表达式、mod.rewrite,上帝知道在 6 小时内还能解决这个问题!所以我希望其他人也能受益)我!)

DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ http://www.colours.com/$1   [R=301,L]

可能还有一些其他指令值得添加,例如使其不重写隐藏文件,防止其循环等,但我还没有足够了解 mod.rewrite 来使这个干净。然而它确实可以按原样工作,这就是我主要目标的 99.99999%。

一周后进行编辑。

使用这个解决方案已经有一段时间了,我不得不承认,为了这一点点魔法是要付出代价的。正如您所了解的那样,我不是专家,但在我看来,此解决方案会导致浏览器请求所涉及的每个资源两次。如果您的网站很小并且效率影响不明显,这可能不是问题,但如果您想坚持最佳加载时间,那么您可能需要权衡使用此解决方案与将链接重新编码为的利弊更直接的东西。只是让你知道!

I cracked it!
It required 1 pint of blood, 2 gallons of sweat and 3 magnums of tears, but I finally cracked it.
The secret is to use mod.rewrite as I was starting to suspect. You can indeed not address areas above the root, but there are other ways of refering to such locations, specifically you can refer to files or directories that 'don't exist' using the ReWrite conditions here:

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

So for example, when you want to refer to the style sheet directory that was at a higher level than the currect context, you would refer to ../css, but after you've made the current context into it's own sub-domain, ../css no longer exists. That means you can make a match with one or both of the above rewrite conditions. Now it's just a case of pointng the client to the (e.g.) css directory via the original domain, www.colour.com/css, in my example. Here, for completeness, and to help any other poor soul from killing themselves over this (I had to learn regular expressions, mod.rewrite and Lord knows what else in 6 hours to solve this one! so I hope someone else benefits as well as me!)

DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ http://www.colours.com/$1   [R=301,L]

There may be some other directives worth adding such as to make it not rewrite hidden files, to prevent it looping etc but I haven't learn enough about mod.rewrite yet to make this squeeky clean. However it does work as it is and that is 99.99999% of my main goal.

Edit one week on.

Having lived with this solution a while I have to admit that there is a price to pay for this little bit of magic. I'm no expert as you will have gleaned, but it seems to me that this solution leads the browser to request each of the resources involved twice. This might not be a problem if your site is small and the efficiency hit unnoticeable, but if you want to be a stickler for optimum load times, then you might have to weigh up the pro's and con's of using this solution versus recoding your links into something more direct. Just so you know!

清风不识月 2025-01-11 23:08:20

弗兰克,一个小小的调侃:给我们 XYZ“然后我可以发布一个解决方案”与“包括我的网络托管公司在内的每个来源都说不可能做到这一点”不太一致。

您的方法只是排序做您想做的事情,因为像这样的 301 重定向只是将问题传递回客户端的浏览器,告诉它从其他子域重新获取资源,因此它对每个资源执行两个请求 - 这会减慢加载时间在客户端。

您的第一个解决方案是,您可以直接在 srchref 中编码 http://common.colour.com/... 来完成HTML 中的 Strong> 属性。但这有一个缺点,即您需要更改脚本。这种方法实际上很常见,因为许多大容量网站对此类静态资源使用单独的域,并且现在经常使用 CDN 为此。它还具有性能优势,因为浏览器通常会为每个域打开两个流来加载内容,但会将请求并行到单独的流。

您的第二个解决方案是在服务器文件系统中执行此操作。您需要做的是创建 DOCROOT/red/css 到 DOCROOT/css 等的符号链接。如果您使用 PHP 编写脚本,您可以通过在 DOCROOT 中创建并执行临时配置脚本并调用它来添加这些链接来完成此操作(它们在您的 FTP 浏览器中显示为单独的目录副本,但实际上都指向共享资源。您需要使用的关键函数是 symlink() 像这样:

<?php
$root=dirname(__FILE__);
foreach( array( 'red', 'green', 'blue') as $c ){
  foreach( array( 'css', 'js' ) as $d ) {
    symlink( "$root/$c", "$root/$d/$c);
  }
}

您需要调整颜色和资源目录请注意,我刚刚输入了此内容并没有对其进行调试 - 需要额外费用;-)

第三个。解决方案是使用 .htaccess 文件,但进行内部重定向而不是外部重定向。这里 Apache 重写模块将请求直接映射到不同的路径/文件名。我假设您的 cpanel 允许您将 XXX.colour.com 映射到 DOCROOT/xxx 等,以便“GET /css/sheet.css” 到 HTTP_HOST red.colour.com 被处理为 DOCROOT/red/css/sheet.css。如何处理此问题取决于您是在 DOCROOT 使用单个 .htaccess 还是单独的 red/.htaccess 等。如果是前者,您可以使用如下规则:

RewriteEngine On
RewriteBase   /

RewriteRule (red|green|blue)/(css|js|images)/(.*)  /$2/$3 [L]

您确实想要此处规则目标字符串的前导 / 。

尝试一下这些,希望这会有所帮助:-)

Frank, one slight tease: give us XYZ "then I can post a solution" isn't quite consistent with "every source including my web hosting company said it wasn't possible to do this".

Your approach is only sort doing what you want because a 301 redirect like this just passes the problem back to the client's browser telling it to refetch the resource from your other subdomain so it does two request for each resource -- which slows down the load time at the client.

Your first solution is that you could have done by just directly coding http://common.colour.com/... in your src and href attributes in your HTML. But this has the downside that you need to change your scripts. This approach is actually quite common as many high volume sites use a separate domain for such static resource and now often use a CDN for this. It can also have performance advantages as browsers will typically open two streams per domain for loading content but parallel up requests to separate streams.

Your second solution is to do this within your servers filesystem. What you need to do is to create symbolic links for DOCROOT/red/css to DOCROOT/css etc. If you are scripting in PHP you can do this by creating and executing a temporary configuration script in your DOCROOT and invoke it to add these links (which appear in your FTP browser as separate directory copies but in fact all point to shared resources. The key function that you need use is symlink() like this:

<?php
$root=dirname(__FILE__);
foreach( array( 'red', 'green', 'blue') as $c ){
  foreach( array( 'css', 'js' ) as $d ) {
    symlink( "$root/$c", "$root/$d/$c);
  }
}

You'd need to tweak the colours and resource directories. Note that I've just typed this in and not debugged it -- costs extra ;-)

The third solution is to use an .htaccess file, but to do internal redirects rather than external ones. Here the Apache rewrite module maps the request directly to a different path/filename. I assume that your cpanel allows you to map XXX.colour.com to DOCROOT/xxx etc. so that a "GET /css/sheet.css" to a HTTP_HOST red.colour.com gets processed as DOCROOT/red/css/sheet.css. How you approach this depends on whether you use a single .htaccess at DOCROOT or separate red/.htaccess etc. If the former you can use a rule like:

RewriteEngine On
RewriteBase   /

RewriteRule (red|green|blue)/(css|js|images)/(.*)  /$2/$3 [L]

You do want the leading / on the rule target string here.

Try these out, and hope this helps :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文