多个web.config文件

时间:2019-07-11 15:20:45

标签: mod-rewrite url-rewriting iis-7

因此,我正在将应用程序从xampp迁移到iis-7,并希望在iis中重现URL重写行为。

我的项目结构:

-- root  
  |-- app (!-- contains my aplication code)  
  |-- public (!-- contains my front end assets) 

我的xampp网址重写行为:

调用“ myhost /” =>“ myhost / public / index.php”
调用“ myhost / existing_dir_or_file / file.something” =>“ myhost / public / mydir / myfile.something”
调用“ myhost / not_a_file_or_dir /某物” =>“ myhost / public / index.php?url = not_a_file_or_dir /某物”

要在xampp中执行此操作,我使用两个.htaccess文件,一个在根目录中用于前两个规则,另一个在公共目录中并包含第三个规则。

我的.htaccess在根文件夹中

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteRule ^$ public/ [L]
  RewriteRule (.*) public/$1 [L]
</IfModule>

.htaccess在根/公共文件夹中

<IfModule mod_rewrite.c>
  Options -Multiviews
  RewriteEngine On
  RewriteBase /host/public
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule  ^(.+)$ index.php?url=$1 [QSA,L]
</IfModule>
根中的

web.config文件(随iis导入)

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Imported Rule 1" stopProcessing="true">
                    <match url="^$" ignoreCase="false" />
                    <action type="Rewrite" url="public/" />
                </rule>
                <rule name="Imported Rule 2" stopProcessing="true">
                    <match url="(.*)" ignoreCase="false" />
                    <action type="Rewrite" url="public/{R:1}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

root / public中的web.config文件(通过iis导入)

<rewrite>
  <rules>
    <rule name="Imported Rule 1" stopProcessing="true">
      <match url="^(.+)$" ignoreCase="false" />
      <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
      </conditions>
      <action type="Rewrite" url="index.php?url={R:1}" appendQueryString="true" />
    </rule>
  </rules>
</rewrite>

正如我测试的那样,如果我仅设置根目录/公共规则并要求输入“ hostname / public / pages / index”之类的内容,它就可以正常工作并使我进入“ hostname / public / index.php?url = pages / index” “

但是,如果我将2条最基本的规则用于获得所需的完整行为,则会显示此页面404 Not Found

0 个答案:

没有答案