Select Page
iis web 將 http 協定導引成 https 協定(http to https)

iis web 將 http 協定導引成 https 協定(http to https)

現在的網站一定都要有https的協定才會是安全的,SEO的分數也才會高,在IIS內可以直接透過 URL rewrite 來將 http 連線都轉換成 https 連線,過程比 nginx 要麻煩一點,但有圖形化的介面設定,也是蠻容易上手的

打開 IIS Manager 找到 URL Rewrite

先假設你已經完成了 URL Rewrite 的安裝,則可以在介面中看到 URL Rewrite的設定

建立新的規則 (Add Rule)

在右邊的 Actions 選擇 Add Rule(s),並且選擇 Blank rule

建立 Inbound Rule

如下圖,下拉方塊中選擇 Match URL,並且在 Pattern 中填入 (.*) ,然後移到下方 Conditions 選擇 Add

並且在 Condition input 填入 {HTTPS} ,在 Pattern 中填入 ^OFF$ ,下拉選單中則是選擇 Matches the Pattern

設定 Action

輸入網址的條件設定完之後,要來設定符合的網址要如何處理,如何做動作,參考下圖,Action type 的下拉選單,選擇 Redirect

並且在 Redirect URL 中,填入 https://{HTTP_HOST}/{R:1} ,並且到 Redirect type 下拉選單中選擇 Permanent(301) 後即可以存檔 (Apply)

Web.Config 中的 rewrite

用 iis manager 設定完畢後,你就可以在 web.config 中看到剛剛所做的設定

        <rewrite>
            <rules>
                <clear />
                <rule name="http2https" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{HTTPS}" pattern="^OFF$" />
                    </conditions>
                    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
                </rule>
            </rules>
        </rewrite>

參考資料

https://aboutssl.org/iis-redirection-http-to-https/

windows server iis 下的 wordpress web.config 設定

在 windows server 的 iis 下,預設情況下安裝 php 架構的 wordpress 總是會遇到很多的困難,像是如果遭遇到想要訪問 wordpress 目錄下的目錄資料,例如:https://rain.tips/uploads/,會跟你說找不到資料,原因是wordpress所有的入口要先透由 index.php 去做路由,解決方案則是用 web.config 去指定路由要透過 index.php 即可

請在web.config檔案中添加 rewrite rules

    <rewrite>
      <rules>
			<rule name="WordPress: https://yoururl.com" patternSyntax="Wildcard">
				<match url="*"/>
					<conditions>
						<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
						<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
					</conditions>
				<action type="Rewrite" url="index.php"/>
			</rule></rules>
    </rewrite>

最後會長成這樣子

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
			<rule name="WordPress: https://yoururl.com" patternSyntax="Wildcard">
				<match url="*"/>
					<conditions>
						<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
						<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
					</conditions>
				<action type="Rewrite" url="index.php"/>
			</rule></rules>
    </rewrite>
  </system.webServer>
</configuration>

同場加映

IIS 下的 WordPress 如何移除 index.php 路徑

IIS 下的 WordPress 如何移除 index.php 路徑

很少人使用 IIS 架設 wordpress ,因為文件不好找,例外狀況又多,這次又發現 WordPress 的 Route 規則怪怪的,會在所有路徑中出現 index.php? ,這才意識到之前用 nginx 時候這些規則都早就解決,只要研究如何處理這樣的困境,研究之後得到有三個要點。

  • IIS 需要有 URL rewrite 擴充
  • WordPress 後台要去「設定->永久連結」設定自訂結構
  • WordPress 中的 web.config 需要加入 rewrite rule

先安裝 IIS 的 URL Rewrite
回到 WordPress 的後台設定永久連結
更改 web.config 設定 rewrite rule
	<rewrite>
		<rules>
			<rule name="WordPress: https://yourdomain.com" patternSyntax="Wildcard">
				<match url="*"/>
					<conditions>
						<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
						<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
					</conditions>
				<action type="Rewrite" url="index.php"/>
			</rule></rules>
    </rewrite>

Apache and Nginx 的設定作法

https://www.php.cn/cms/wordpress/459657.html