How can I create a redirection in IIS to always redirect to https://www.example.com and remove index.php from the URL?

huangapple go评论53阅读模式
英文:

How can I create a redirection in IIS to always redirect to https://www.example.com and remove index.php from the URL?

问题

我想创建一个IIS服务器的URL重定向,但它不起作用。我希望无论何种情况下,如果一个人输入:(http://example.com、https://example.com、www.example.com、http://www.example.com),都将重定向到https://www.example.com。您还应该从URL中删除index.php。还有一些例外的库,您应该忽略。

这是我尝试的方式:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <defaultDocument>
      <files>
        <add value="default.php"/>
      </files>
    </defaultDocument>
    <rewrite>
      <rules>
        <rule name="RuleRemoveIndex" 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" />
            <add input="{REQUEST_URI}" matchType="Pattern" pattern="*/folder1*" ignoreCase="true" negate="true" />
            <add input="{REQUEST_URI}" pattern="*/folder2*" negate="true" />
          </conditions>
          <action type="Rewrite" url="index.php/{R:1}" appendQueryString="true"/>
        </rule>
        <rule name="RedirectNonWwwToWww" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^example.com$" />
          </conditions>
          <action type="Redirect" url="https://www.example.com/{R:0}" redirectType="Permanent" />
        </rule>    
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

这是您提供的XML配置文件,已经翻译成中文。

英文:

I want to create an IIS server url redirection, but it doesn't work. I would like that if a person enters: (http://example.com, https://example.com, www.example.com, http://www.example.com) in any case, redirect to https://www.example.com You should also cut index.php from the url. And there are a few exception libraries that you should ignore.

This is how I tried it

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;configuration&gt;
  &lt;system.webServer&gt;
    &lt;defaultDocument&gt;
      &lt;files&gt;
        &lt;add value=&quot;default.php&quot;/&gt;
      &lt;/files&gt;
    &lt;/defaultDocument&gt;
    &lt;rewrite&gt;
      &lt;rules&gt;
            &lt;rule name=&quot;RuleRemoveIndex&quot; stopProcessing=&quot;true&quot;&gt;
                &lt;match url=&quot;^(.*)$&quot; ignoreCase=&quot;false&quot; /&gt;
                &lt;conditions&gt;
                    &lt;add input=&quot;{REQUEST_FILENAME}&quot; matchType=&quot;IsFile&quot; ignoreCase=&quot;false&quot; negate=&quot;true&quot; /&gt;
                    &lt;add input=&quot;{REQUEST_FILENAME}&quot; matchType=&quot;IsDirectory&quot; ignoreCase=&quot;false&quot; negate=&quot;true&quot; /&gt;
                    &lt;add input=&quot;{REQUEST_URI}&quot; matchType=&quot;Pattern&quot; pattern=&quot;*/folder1*&quot; ignoreCase=&quot;true&quot; negate=&quot;true&quot; /&gt;
                    &lt;add input=&quot;{REQUEST_URI}&quot; pattern=&quot;*/folder2*&quot; negate=&quot;true&quot; /&gt;
                &lt;/conditions&gt;
                &lt;action type=&quot;Rewrite&quot; url=&quot;index.php/{R:1}&quot; appendQueryString=&quot;true&quot;/&gt;
            &lt;/rule&gt;
            &lt;rule name=&quot;RedirectNonWwwToWww&quot; stopProcessing=&quot;true&quot;&gt;
              &lt;match url=&quot;.*&quot; /&gt;
              &lt;conditions&gt;
                &lt;add input=&quot;{HTTP_HOST}&quot; pattern=&quot;^example.com$&quot; /&gt;
              &lt;/conditions&gt;
              &lt;action type=&quot;Redirect&quot; url=&quot;https://www.example.com/{R:0}&quot; redirectType=&quot;Permanent&quot; /&gt;
            &lt;/rule&gt;    
      &lt;/rules&gt;
    &lt;/rewrite&gt;
  &lt;/system.webServer&gt;
&lt;/configuration&gt;

答案1

得分: 1

你可以尝试这个规则:

<rule name="RemoveIndex" stopProcessing="true">
    <match url="^(.*)$" />
    <conditions logicalGrouping="MatchAll">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
        <add input="{REQUEST_FILENAME}.php" matchType="IsFile" ignoreCase="false" />
    </conditions>
    <action type="Rewrite" url="index.php" />
</rule>

<rule name="Force WWW and SSL" enabled="true" stopProcessing="true">
    <match url="^(.*)$" />
    <conditions logicalGrouping="MatchAny">
        <add input="{HTTP_HOST}" pattern="^[^www]" />
        <add input="{HTTPS}" pattern="off" />
    </conditions>
    <action type="Redirect" url="https://www.example.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>

希望这有帮助!

英文:

You can try this rule:

 &lt;rule name=&quot;RemoveIndex&quot; stopProcessing=&quot;true&quot;&gt;
    &lt;match url=&quot;^(.*)$&quot; /&gt;
       &lt;conditions logicalGrouping=&quot;MatchAll&quot;&gt;
          &lt;add input=&quot;{REQUEST_FILENAME}&quot; matchType=&quot;IsFile&quot; ignoreCase=&quot;false&quot; negate=&quot;true&quot; /&gt;
          &lt;add input=&quot;{REQUEST_FILENAME}.php&quot; matchType=&quot;IsFile&quot; ignoreCase=&quot;false&quot; /&gt;
       &lt;/conditions&gt;
    &lt;action type=&quot;Rewrite&quot; url=&quot;index.php&quot; /&gt;
 &lt;/rule&gt;

 &lt;rule name=&quot;Force WWW and SSL&quot; enabled=&quot;true&quot; stopProcessing=&quot;true&quot;&gt;
    &lt;match url=&quot;^(.*)$&quot; /&gt;
       &lt;conditions logicalGrouping=&quot;MatchAny&quot;&gt;
          &lt;add input=&quot;{HTTP_HOST}&quot; pattern=&quot;^[^www]&quot; /&gt;
          &lt;add input=&quot;{HTTPS}&quot; pattern=&quot;off&quot; /&gt;
       &lt;/conditions&gt;
   &lt;action type=&quot;Redirect&quot; url=&quot;https://www.example.com/{R:1}&quot; appendQueryString=&quot;true&quot; redirectType=&quot;Permanent&quot; /&gt;
&lt;/rule&gt;

huangapple
  • 本文由 发表于 2023年5月25日 06:13:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76327727.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定