Select Page

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