WordPress各种伪静态规则大全

  • A+
所属分类:Linux架构

WordPress 各种伪静态规则大全(IIS6/IIS7.5/Apache/Nginx/win2003/win2008)

不少朋友总是询问 WordPress 如何添加伪静态规则,总结如下 IIS6/IIS7.5/Apache/Nginx/win2003/win2008 四种环境下的伪静态规则,希望对大家有所帮助。其中 IIS7.5 的规则是目前最完美的!

明明是六种为什么要说是四种呢?其实 windows 的主机在 2003 时为 IIS6,而 2008 以后升级为了 IIS7.5 所以 IIS6 的规则在 IIS7.5 不通用哦,其实我以前也很纠结的。弄了半天规则才发现,悲催了

检测主机是否支持伪静态的方法:在 WP 后台 > 设置 > 固定链接,设置为 非默认带? 的那种结构,然后访问任何一篇文章,如果出现 404 错误,说明你的主机当前不支持 WordPress 伪静态。

WordPress各种伪静态规则大全

IIS7.5 完美伪静态规则

IIS 环境是 Windows 主机常用的服务器环境,但网上很多规则其实并不完美,很多地方依旧有些无法设置的地方,但这个却可以,新建两个 txt 文件,将下面的代码分别添加到文件中:

第一个 web.config

<configuration>

<system.webServer>

<rewrite>

<rules>

<rule name="ChineseURL" stopProcessing="true">

<match url="^(tag|category)/(.*)$" />

<action type="Rewrite" url="ihuanurl.php"/>

</rule>

<rule name="WordPress" 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>

第二个辅助文件 ihuanurl.php:

<?php

// IIS Mod-Rewrite

if (isset($_SERVER['HTTP_X_ORIGINAL_URL'])) {

$_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_ORIGINAL_URL'];

}

// IIS Isapi_Rewrite

else if (isset($_SERVER['HTTP_X_REWRITE_URL'])) {

$_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_REWRITE_URL'];

}

else

{

// Use ORIG_PATH_INFO if there is no PATH_INFO

if ( !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']) ) {

if ( $_SERVER['PATH_INFO'] == $_SERVER['SCRIPT_NAME'] )

$_SERVER['REQUEST_URI'] = $_SERVER['PATH_INFO'];

else

$_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] . $_SERVER['PATH_INFO'];

}

// Append the query string if it exists and isn't null

if (isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])) {

$_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING'];

}

}

require("index.php");

?>

然后保存文件,上传到 WordPress 站点的根目录即可。

IIS6 伪静态规则

IIS 环境是 Windows 主机常用的服务器环境,新建一个 txt 文件,将下面的代码添加到文件中:

[ISAPI_Rewrite]

# Defend your computer from some worm attacks

#RewriteRule .*(?:global.asa|default.ida|root.exe|..).* . [F,I,O]

# 3600 = 1 hour

CacheClockRate 3600

RepeatLimit 32

 

# Protect httpd.ini and httpd.parse.errors files

# from accessing through HTTP

# Rules to ensure that normal content gets through

RewriteRule /tag/(.*) /index.php?tag=$1

RewriteRule /software-files/(.*) /software-files/$1 [L]

RewriteRule /images/(.*) /images/$1 [L]

RewriteRule /sitemap.xml /sitemap.xml [L]

RewriteRule /favicon.ico /favicon.ico [L]

# For file-based WordPress content (i.e. theme), admin, etc.

RewriteRule /wp-(.*) /wp-$1 [L]

# For normal WordPress content, via index.php

RewriteRule ^/$ /index.php [L]

RewriteRule /(.*) /index.php/$1 [L]

然后另存为 httpd.ini 文件,上传到 WordPress 站点的根目录即可。

Apache 伪静态规则

Apache 是 Linux 主机下常见的环境,现在一般的 Linux 虚拟主机都采用这种环境。新建一个 htaccess.txt 文件,添加下面的代码:

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteBase /

RewriteRule ^index.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /index.php [L]

</IfModule>

然后上传到 WordPress 站点的根目录,重命名为 .htaccess 即可

Nginx 伪静态规则

Nginx 环境一般是 Linux 主机 VPS 或服务器用户用的比较多,这些用户一般都会自己配置 Nginx,或者有专门的人帮你配置,打开 nginx.conf 或者某个站点的配置环境,比如 wpdaxue.com.conf(不同人配置的不一样),在  server   { } 大括号里面添加下面的代码:

location / {

if (-f $request_filename/index.html){

rewrite (.*) $1/index.html break;

}

if (-f $request_filename/index.php){

rewrite (.*) $1/index.php;

}

if (!-f $request_filename){

rewrite (.*) /index.php;

}

}

保存,重启 Nginx 即可。

(IIS6/IIS7.5/Apache/Nginx/win2003/win2008)

不少朋友总是询问 WordPress 如何添加伪静态规则,总结如下 IIS6/IIS7.5/Apache/Nginx/win2003/win2008 四种环境下的伪静态规则,希望对大家有所帮助。其中 IIS7.5 的规则是目前最完美的!

明明是六种为什么要说是四种呢?其实 windows 的主机在 2003 时为 IIS6,而 2008 以后升级为了 IIS7.5 所以 IIS6 的规则在 IIS7.5 不通用哦,其实我以前也很纠结的。弄了半天规则才发现,悲催了

检测主机是否支持伪静态的方法:在 WP 后台 > 设置 > 固定链接,设置为 非默认带? 的那种结构,然后访问任何一篇文章,如果出现 404 错误,说明你的主机当前不支持 WordPress 伪静态。

WordPress各种伪静态规则大全

IIS7.5 完美伪静态规则

IIS 环境是 Windows 主机常用的服务器环境,但网上很多规则其实并不完美,很多地方依旧有些无法设置的地方,但这个却可以,新建两个 txt 文件,将下面的代码分别添加到文件中:

第一个 web.config

<configuration>

<system.webServer>

<rewrite>

<rules>

<rule name="ChineseURL" stopProcessing="true">

<match url="^(tag|category)/(.*)$" />

<action type="Rewrite" url="ihuanurl.php"/>

</rule>

<rule name="WordPress" 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>

第二个辅助文件 ihuanurl.php:

<?php

// IIS Mod-Rewrite

if (isset($_SERVER['HTTP_X_ORIGINAL_URL'])) {

$_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_ORIGINAL_URL'];

}

// IIS Isapi_Rewrite

else if (isset($_SERVER['HTTP_X_REWRITE_URL'])) {

$_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_REWRITE_URL'];

}

else

{

// Use ORIG_PATH_INFO if there is no PATH_INFO

if ( !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']) ) {

if ( $_SERVER['PATH_INFO'] == $_SERVER['SCRIPT_NAME'] )

$_SERVER['REQUEST_URI'] = $_SERVER['PATH_INFO'];

else

$_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] . $_SERVER['PATH_INFO'];

}

// Append the query string if it exists and isn't null

if (isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])) {

$_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING'];

}

}

require("index.php");

?>

然后保存文件,上传到 WordPress 站点的根目录即可。

IIS6 伪静态规则

IIS 环境是 Windows 主机常用的服务器环境,新建一个 txt 文件,将下面的代码添加到文件中:

[ISAPI_Rewrite]

# Defend your computer from some worm attacks

#RewriteRule .*(?:global.asa|default.ida|root.exe|..).* . [F,I,O]

# 3600 = 1 hour

CacheClockRate 3600

RepeatLimit 32

 

# Protect httpd.ini and httpd.parse.errors files

# from accessing through HTTP

# Rules to ensure that normal content gets through

RewriteRule /tag/(.*) /index.php?tag=$1

RewriteRule /software-files/(.*) /software-files/$1 [L]

RewriteRule /images/(.*) /images/$1 [L]

RewriteRule /sitemap.xml /sitemap.xml [L]

RewriteRule /favicon.ico /favicon.ico [L]

# For file-based WordPress content (i.e. theme), admin, etc.

RewriteRule /wp-(.*) /wp-$1 [L]

# For normal WordPress content, via index.php

RewriteRule ^/$ /index.php [L]

RewriteRule /(.*) /index.php/$1 [L]

然后另存为 httpd.ini 文件,上传到 WordPress 站点的根目录即可。

Apache 伪静态规则

Apache 是 Linux 主机下常见的环境,现在一般的 Linux 虚拟主机都采用这种环境。新建一个 htaccess.txt 文件,添加下面的代码:

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteBase /

RewriteRule ^index.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /index.php [L]

</IfModule>

然后上传到 WordPress 站点的根目录,重命名为 .htaccess 即可

Nginx 伪静态规则

Nginx 环境一般是 Linux 主机 VPS 或服务器用户用的比较多,这些用户一般都会自己配置 Nginx,或者有专门的人帮你配置,打开 nginx.conf 或者某个站点的配置环境,比如 wpdaxue.com.conf(不同人配置的不一样),在  server   { } 大括号里面添加下面的代码:

location / {

if (-f $request_filename/index.html){

rewrite (.*) $1/index.html break;

}

if (-f $request_filename/index.php){

rewrite (.*) $1/index.php;

}

if (!-f $request_filename){

rewrite (.*) /index.php;

}

}

保存,重启 Nginx 即可。

weinxin
我的微信
这是我的微信扫一扫

发表评论

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