htaccess子目录作为根目录

htaccess子目录作为根目录

本文介绍了htaccess子目录作为根目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这里有一些类似的问题,因此请原谅我无法将其组合在一起-我希望只有在用户访问根目录时才显示子目录(主站点),而其余站点仍保留为

I know there is a few questions like this here, so forgive me for not being able to piece this together - I want a subdirectory (mainsite) to display when someone visits the root only, while the rest of the site remains as normal.

结构为:

www.mysite.com
  --globalcss
  --sites--mainsite--index.php
                   --anotherpage.php
                   --css--style.css
         --anothersite
         --anothersite

我也在尝试在mainsite索引上获取相对的CSS链接一旦主站点索引显示在根目录中,页面(../../globalcss/global.css)便可以正常工作。有一次,我的页面正常工作,但是css链接错误地显示为 sites / mainsite / globalcss / ,而忽略了 ../.。 / (如果可能,我宁愿让它们保持相对,因为我的本地主机根目录不匹配。)

I'm also trying to get the relative css links on the mainsite index page (../../globalcss/global.css) to work once the mainsite index is displaying in the root. At one point I had the page working, but the css links were incorrectly showing as sites/mainsite/globalcss/, ignoring the ../../ (I'd rather keep them relative if possible as my localhost root doesn't match).

这是我到目前为止所拥有的:

Here is what I have so far:

RewriteEngine on
# attempting to get only requests in the root directory
RewriteCond %{REQUEST_URI} !(sites|globalcss)
# show the mainsite content instead
RewriteRule ^(.*)$ /sites/mainsite/$1 [L]

谢谢

推荐答案

因此,有人只访问根 ,我假设您的意思是 http://www.mysite.com/ ,而不是 http://www.mysite.com/anotherpage.php 。在这种情况下,

So by "someone visits the root only", I assume you mean http://www.mysite.com/, and not http://www.mysite.com/anotherpage.php. If that's the case then:

RewriteEngine on
RewriteCond %{REQUEST_URI} !globalcss
RewriteCond %{REQUEST_URI} !^/sites/mainsite
RewriteRule ^$ /sites/mainsite/ [L]

如果您实际上引用的不是子目录,例如: http://www.mysite.com/ 可以, http://www.mysite.com/a-file.php 可以,但是 http://www.mysite.com/css/ 不应该被重写,则规则应如下所示:

If you're actually referring to anything that isn't a subdirectory, as in: http://www.mysite.com/ is ok, http://www.mysite.com/a-file.php is ok, but http://www.mysite.com/css/ should not be rewritten, then the rule needs to look like:

RewriteRule ^([^/]*)$ /sites/mainsite/$1 [L]

其他都一样。

设置正则表达式的方式:

The way that you have your regex setup:

RewriteRule ^(.*)$ /sites/mainsite/$1 [L]

重写除 globalcss以外的所有内容。而且,您的要求在网站的其余部分仍保持正常状态时,这似乎并没有引起您的兴趣。

rewrites everything except "globalcss". And that doesn't seem to jive at all with your request that "while the rest of the site remains as normal."

唯一的事情是我的站点/主站点/ css现在不起作用,因为它相对于根目录而不是原始位置。我该如何解决?我那里也有一个js文件夹,我认为它也有同样的问题

浏览器看起来相对于根目录,因为仅此而已知道。它只知道我去这里: http://mysite.com/ ,然后看到链接 css / some_file.css ,因此它唯一可以做的就是进入 http://mysite.com/css/some_file.css ,浏览器对仅在服务器上存在的所有这些重写或别名一无所知。

The browser looks relative to the root because that's all it knows. All it knows is "I go here: http://mysite.com/ and I see link "css/some_file.css", So the only thing it can do is go to http://mysite.com/css/some_file.css, the browser knows nothing of all these rewrites or aliases which exists only on the server.

如果您希望css能够被访问,则需要一个规则,因为好(这样做确实是一种可怕的方法):

If you want css to be able to get accessed, then you need a rule for that as well (this is really a terrible, terrible way of doing all of this):

RewriteEngine on
RewriteCond %{REQUEST_URI} !globalcss
RewriteCond %{REQUEST_URI} !^/sites/mainsite/css
RewriteRule ^css/(.*)$ /sites/mainsite/css/$1 [L]

可能也类似于js

这篇关于htaccess子目录作为根目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 15:03