htaccess subdirectory as root directory

2019-09-03 20:03发布

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.

structure is:

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

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]   

Thanks

标签: .htaccess
1条回答
对你真心纯属浪费
2楼-- · 2019-09-03 20:36

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] 

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] 

and everything else is the same.

The way that you have your regex setup:

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

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."


only thing is my sites/mainsite/css is now not working, as it is looking relative to the root, not original location. How can I work around this? I also have a js folder there too, which I assume has the same issue

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.

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] 

Probably something similar to js also

查看更多
登录 后发表回答