IIS document root in subfolder

旧城冷巷雨未停 提交于 2021-02-07 09:16:13

问题


Hey, I have a PHP Windows Azure project but I am new to IIS. I have my PHP libs in root folder and client files like index.php, style sheets or javascripts in a subfolder called document_root.

How can I set up IIS 7 (probably using Web.config) to use the document_root as the default folder for the site (making the root invisible) so i can call mydomain.tld/ instead of mydomain.tld/document_root/

I have used:

<defaultDocument>
  <files>
    <clear />
    <add value="document_root/index.php" />
  </files>
</defaultDocument>

Which works fine but it accesses only the index.php and cant find any files like /document_root/css/default.css (the relative address is css/default.css)


回答1:


There are 2 ways to do this. The easy route is to use the URL Rewriting module included in IIS7. An example to redirect requests to the Public folder (and disallow direct access to that folder) :

<system.webServer>
    <rewrite>
        <rules>
            <clear />
            <rule name="blockAccessToPublic" patternSyntax="Wildcard" stopProcessing="true">
                <match url="*" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    <add input="{URL}" pattern="/public/*" />
                </conditions>
                <action type="CustomResponse" statusCode="403" statusReason="Forbidden: Access is denied." statusDescription="You do not have permission to view this directory or page using the credentials that you supplied." />
            </rule>
            <rule name="RewriteRequestsToPublic" stopProcessing="true">
                <match url="^(.*)$" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                </conditions>
                <action type="Rewrite" url="public/{R:0}" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

The other way is to change the IIS settings once your PHP application is deployed. To do that you need to override the OnStart method of your web role, but I think the first solution will suit your needs.




回答2:


Modify the BASIC SETTINGS and WEBSITE BINDINGS. If you only have one website, you will probably just want to edit the BASIC SETTINCGS for the DEFAULT WEBSITE:

  1. Open IIS Manager
  2. Click on Default Web Site
  3. Under Actions (right hand side), select Basic Settings
  4. Edit Physical Location.

If you are testing several websites on one development machine (with different "DOCUMENT_ROOT" folders), right-click on each website in the list, Edit Bindings, and set the various websites to run under different Port Numbers. Then, you can access the various websites using (for example): http-colon-slash-slash localhost:81




回答3:


I am coming at this from the other angle (knowing little PHP but IIS and Azure well). I don't know why your project is structured the way it is, so I'll just assume it has to be that way for some reason. I would just like to point out that in my experience it's a little atypical to have a subfolder that you want to work as the root and not just have it at the root.

Regardless, the techniques I would look at are called Rewriting or Routing, which lets you change from standard the way that URLs are parsed and passed to executing pages. Stackoverflow has lots of posts about it that you might like to trawl, here's a good one IIS URL Rewriting vs URL Routing which gives an answer as to two different types of routing and where to use which.

You can do it in web.config, and it should let you achieve what you want.



来源:https://stackoverflow.com/questions/5796085/iis-document-root-in-subfolder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!