zl程序教程

您现在的位置是:首页 >  其他

当前栏目

IIS环境下PHPrewrite重写设置(支持中文参数)

中文重写 环境 设置 支持 参数 iis
2023-06-13 09:15:32 时间

在网站根目录下加入:

Web.Config:

<?xmlversion="1.0"encoding=”UTF-8″?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rulename="cnUrl"stopProcessing="true">
<matchurl="!^(index\.php|images|assets|robots\.txt)"/>
<actiontype="Rewrite"url="cnurl.php"/>
</rule>
<rulename="Default"patternSyntax="Wildcard">
<matchurl="*"/>
<conditions>
<addinput="{REQUEST_FILENAME}"matchType="IsFile"negate="true"/>
<addinput="{REQUEST_FILENAME}"matchType="IsDirectory"negate="true"/>
</conditions>
<actiontype="Rewrite"url="index.php"/>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

cnurl.php:

<?php
if(isset($_SERVER["HTTP_X_ORIGINAL_URL"])){
//IISMod-Rewrite
$_SERVER["REQUEST_URI"]=$_SERVER["HTTP_X_ORIGINAL_URL"];
}elseif(isset($_SERVER["HTTP_X_REWRITE_URL"])){
//IISIsapi_Rewrite
$_SERVER["REQUEST_URI"]=$_SERVER["HTTP_X_REWRITE_URL"];
}else{
//UseORIG_PATH_INFOifthereisnoPATH_INFO
(!isset($_SERVER["PATH_INFO"])&&isset($_SERVER["ORIG_PATH_INFO"]))&&($_SERVER["PATH_INFO"]=$_SERVER["ORIG_PATH_INFO"]);
//SomeIIS+PHPconfigurationsputsthescript-nameinthepath-info(Noneedtoappendittwice)
if(isset($_SERVER["PATH_INFO"])){
($_SERVER["PATH_INFO"]==$_SERVER["SCRIPT_NAME"])?($_SERVER["REQUEST_URI"]=$_SERVER["PATH_INFO"]):($_SERVER["REQUEST_URI"]=$_SERVER["SCRIPT_NAME"].$_SERVER["PATH_INFO"]);
}
//Appendthequerystringifitexistsandisn"tnull
(isset($_SERVER["QUERY_STRING"])&&!empty($_SERVER["QUERY_STRING"]))&&($_SERVER["REQUEST_URI"].="?".$_SERVER["QUERY_STRING"]);
}
require("index.php");

支持IIS环境下跑各种开源PHP项目,如:Wordpress、Emlog、Typecho等。