简体   繁体   中英

Rewrite rule “URL as parameter” in IIS web.config (returns error 500)

I mange "URL as parameter" with Apache .htaccess using flag [B]

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{THE_REQUEST} \s/+(\S+)
RewriteRule (.*) index.php?i=%1 [B,L]

Unfortunately can't figure out how to do the same in IIS web.config

<configuration>
   <system.webServer>
      <defaultDocument enabled="true">
         <files>      
            <clear/>              
            <add value="index.php"/>
         </files>
      </defaultDocument>
      <modules runAllManagedModulesForAllRequests="true"/>
      <rewrite>
          <rules>
                <rule name="rule" stopProcessing="true">
                    <match url="^(.*)$" ignoreCase="false" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.php?i={R:1}" appendQueryString="true" />
                </rule>
            </rules>
        </rewrite>
   </system.webServer>
</configuration>

Lets say web.config is under http://localhost/dir/web.config ...

Results: Opening http://localhost/dir/ and http://localhost/dir/example executes index.php correctly, but accessing http://localhost/dir/i/http://example.org returns error 500 The page cannot be displayed because an internal server error has occurred.

Expected: http://localhost/dir/i/http://example.org must execute index.php with $_GET['i'] value dir/i/http://example.org

It seems IIS doesn't allow to use in URL :/ ... Any workaround/fix for it?

Tools like http://www.htaccesstowebconfig.com doesn't convert .htaccess correctly, your help/advice would be very appreciated.

The only way I find the way to respond URLs containing :/ and returning multiple slash value/query ://... (example URL http://localhost/dir/i/http://example.org ) was by using UNENCODED_URL ; fallowing answer:

<rule name="Index" stopProcessing="true">
    <match url=".*" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
    </conditions>
    <action type="Rewrite" url="index.php?i={UNENCODED_URL}" appendQueryString="true" />
</rule>

Notice, it will return value begining / like /dir/i/http://example.org .

Any hacking around to allow special symbols, like

<system.web>
    <httpRuntime requestValidationMode="2.0" requestPathInvalidCharacters="" relaxedUrlToFileSystemMapping="true" AllowRestrictedChars="1" UrlSegmentMaxLength="2048" />
    <pages validateRequest="false" />
</system.web>
<security>
    <requestFiltering allowDoubleEscaping="false" />
</security>

didn't give any benefit in order to solve the issue (no need for it).

Update: It works fine on local Windows 10 machine, but still returns error 500 on Azure servers. No idea why.

ModuleName  IIS Web Core
Notification    BEGIN_REQUEST
HttpStatus  500
HttpReason  Internal Server Error
HttpSubStatus   19
ErrorCode   The specified path is invalid.
 (0x800700a1)
ConfigExceptionInfo \\?\D:\home\site\wwwroot\i\http:\web.config ( 0) :Cannot read configuration file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM