利用htaccess文件可以很好的进行站点伪静态,并且形成的目标地址与真正的静态页面几乎一模一样,如abc.html等,伪静态可以非常好的结合SEO来提高站点的排名,并且也能给人一种稳定的印象。
若要通过htaccess使用伪静态,则必须空间商支持Rewrite模块,该模块负责URL的重写。否则即便是设置好了,也无法使用,并且还有可能出现500错误。
PHP检测是否支持Rewrite模块:
$result = apache_get_modules(); if(in_array('mod_rewrite', $result)) { echo '支持'; } else { echo '不支持'; }
.htaccess文件常见代码
ErrorDocument 404 /404.html <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP_HOST} ^xxc.com [NC] RewriteRule ^(.*)$ http://www.xxc.com/$1 [L,R=301] RewriteRule ^index.html$ /Index.php [QSA] ################################################## RewriteRule ^ajaxwww.html$ /Index.php?Module=Ajax&Action=Index [QSA] #自定义页面START RewriteRule ^about/map.html$ /Index.php?Module=About&Action=Map [QSA] RewriteRule ^about/([a-z]+).html$ /Index.php?Module=About&Action=Help&Alias=$1 [QSA] RewriteRule ^about/([0-9]+).html$ /Index.php?Module=About&Action=HelpInfo&ID=$1 [QSA] #自定义页面END #验证码 RewriteRule ^code/pic.jpg$ /Plugins/Code/Class.DoCode.php [QSA] #SEO RewriteRule ^robots.txt$ /robots.php [QSA] RewriteRule ^sitemap.xml$ /sitemap.php [QSA] </IfModule>