ThinkPHP伪静态规则.htaccess与httpd.ini下隐藏index.php的解决方法

摘要:什么是伪静态?简单说就是将动态的url变成静态的url显示,就叫做伪静态。“伪”就是假的,不是真的静态url,是通过技术手段实现的。用户只要开通无忧php虚拟空间就支持url重写功能,无需其它任何设置,我们使用伪静态的好处,就是可以优化网络引擎,提高网站权重。将网站静态化处理,是网站seo优化非常重要的一个工作。Thinkphp也可以通过技术手段,实现伪静态处理。

       什么是伪静态?简单说就是将动态的url变成静态的url显示,就叫做伪静态。“伪”就是假的,不是真的静态url,是通过技术手段实现的。用户只要开通无忧php虚拟空间就支持url重写功能,无需其它任何设置,我们使用伪静态的好处,就是可以优化网络引擎,提高网站权重。将网站静态化处理,是网站seo优化非常重要的一个工作。Thinkphp也可以通过技术手段,实现伪静态处理。

满足thinkphp伪静态(url重写)条件:

       1、 服务器开启url_rewrite功能,linux空间的php虚拟主机只需要开启apache的mod_rewriet,如果是iis6.0就要安装ISAPI Rewrite模块,apache只要开启Mod_rewrite功能就可以了。

       2、需要编写.htaccess文件(Linux服务器下),windows2003以下服务器空间编辑httpd.ini文件,windows2008以上使用web.config文件。

       本站在linux空间下编写的.htaccess文件采用Thinkphp官方文档,代码如下:

1
2
3
4
5
6
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/ $1  [QSA,PT,L]
</IfModule>

       本站在windows2003以下的服务器空间编写的Httpd.ini文件代码如下:

1
2
3
4
[ISAPI_Rewrite]
CacheClockRate 3600
RepeatLimit 32
RewriteRule /(?!Public)(?! include )(?!Uploads)(.*) /index.php\ //$1 [L]

       注:(?!Public) 是指这些文件夹不需要进行伪静态,因为这些路径是保存图片和css、js文件的。

       本站在windows2008的服务器使用以下代码,保存的文件名为web.config

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version= "1.0"  encoding= "UTF-8" ?>
<configuration>
<system.webServer>
<rewrite>
<rules>
  <rule name= "OrgPage"  stopProcessing= "true" >
  <match url= "^(.*)$"  />
  <conditions logicalGrouping= "MatchAll" >
  <add input= "{HTTP_HOST}"  pattern= "^(.*)$"  />
  <add input= "{REQUEST_FILENAME}"  matchType= "IsFile"  negate= "true"  />
  <add input= "{REQUEST_FILENAME}"  matchType= "IsDirectory"  negate= "true"  />
  </conditions>
  <action type= "Rewrite"  url= "index.php/{R:1}"  />
  </rule>
  </rules>
</rewrite>
</system.webServer>
</configuration>

       根据不同的空间,请将上面的伪静态规则,保存为.htaccesss文件或Httpd.ini或web.config文件,并放到ThinkPHP项目入口文件同级目录下。

       ThinkPHP要修改数据库配置(config.php)文件使网站支持url重写功能,配置代码:

1
'URL_MODEL' =>2, //兼容模式 不支持伪静态时用,支持则改为2