欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

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

程序员文章站 2022-09-25 19:12:29
在网站根目录下加入: web.config: &...

在网站根目录下加入:

web.config:

<?xml version="1.0" encoding=”utf-8″?>
<configuration>
  <system.webserver>
    <rewrite>
     <rules>
      <rule name="cnurl" stopprocessing="true">
       <match url="!^(index\.php|images|assets|robots\.txt)" />
       <action type="rewrite" url="cnurl.php" />
      </rule>
      <rule name="default" 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>

cnurl.php:

<?php
if (isset($_server['http_x_original_url'])) {
  // iis mod-rewrite
  $_server['request_uri'] = $_server['http_x_original_url'];
} else if (isset($_server['http_x_rewrite_url'])) {
  // iis isapi_rewrite
  $_server['request_uri'] = $_server['http_x_rewrite_url'];
} else {
  // use orig_path_info if there is no path_info
  (!isset($_server['path_info']) && isset($_server['orig_path_info'])) && ($_server['path_info'] = $_server['orig_path_info']);
  // some iis + php configurations puts the script-name in the path-info (no need to append it twice)
  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']);
  }
  // append the query string if it exists and isn't null
  (isset($_server['query_string']) && !empty($_server['query_string'])) && ($_server['request_uri'] .= '?' . $_server['query_string']);
}
require("index.php");

支持iis环境下跑各种开源php项目,如:wordpress、emlog、typecho等。