WordPress 的 .htaccess 规则

原文:wpshout.com

我们知道 .htaccess 文件能轻松地强化你的博客,减少带宽并增加可用性。我曾经写过一篇简易的 .htaccess 教程:《四个例子实战讲解.htaccess文件rewrite规则》。如果你还想继续了解 .htaccess 文件,那么不妨看看下面这26个规则:

但是请先记住一条黄金法则:先备份,再折腾!

A – WP- Admin – 管理员页面

你可以限制访问 WP- Admin 页面的 IP 地址

1
2
3
order deny,allow
allow from a.b.c.d # This is your static IP
deny from all

来源 –BlogSecurity.net

B – Blacklist – 黑名单

.htaccess 的一个重要功能是你能把它作为 IP 黑名单来使用:

1
2
3
4
5

order allow,deny
allow from all
deny from 123.456.789

来源 – Perishable Press

C – WP-Config Protection – 保护 Config 页

WP-Config 文件里包含了你的数据库名、数据库用户名与密码。这样的文件你敢让别人看到吗?所以你会需要用 .htaccess 来保护它。

1
2
3
4
5
# protect wpconfig.php
order allow,deny
deny from all

来源 – Josiah Cole

D – Disable Directory Browsing – 禁止浏览目录

WP-Config 文件里包含了你的数据库名、数据库用户名与密码。这样的文件你敢让别人看到吗?所以你会需要用 .htaccess 来保护它。

1
2
# disable directory browsing
Options All -Indexes

来源- Josiah Cole

E – Explanation – 释疑

如果我问你 .htaccess 是什么,我猜你一定很难说明白。老实说,我自个儿也不明白。好在维基百科给了个明确的解释:

.htaccess 是Apache HTTP Server的文件目录系统级别的配置文件的默认的名字。它提供了在主配置文件中定义用户自定义指令的支持。

维基百科也给出了一些特定的例子,你可以访问:http://zh.wikipedia.org/w/index.php?title=.htaccess&variant=zh-cn 了解更多。

F – Feedburner – RSS烧制

Feedburner 是各位博主的好帮手,通过这个例子你能把原来的 RSS 地址转向到烧制后的地址。

1
2
3
4
5
6
7
# temp redirect wordpress content feeds to feedburner

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT}!FeedBurner [NC]
RewriteCond %{HTTP_USER_AGENT}!FeedValidator [NC]
RewriteRule ^feed/?([_0-9a-z-]+)?/?$ http://feeds.feedburner.com/perishablepress [R=302,NC,L]

来源 – Perishable Press

G – Get an RSS Feed on a static page – 获取静态页的RSS

通过下面这个链接,你能学习到利用 .htaccess 获取静态页 RSS 的办法。

链接 – adityaspeaks.com

H – Disable hotlinking – 防止盗链

所谓盗链,就是别人私自用了你服务器上的图片、声音等文件,占用了你的带宽。你可以通过下面这个 .htaccess 规则来阻止盗链:

1
2
3
4
5
6
#disable hotlinking of images with forbidden or custom image option
RewriteEngine on
RewriteCond %{HTTP_REFERER}!^$
RewriteCond %{HTTP_REFERER}!^http://(www\.)?yourdomain.com/.*$ [NC]
#RewriteRule \.(gif|jpg)$ -[F]
#RewriteRule \.(gif|jpg)$ http://www.yourdomain.com/stealingisbad.gif [R,L]

来源 – Josiah Cole

I – Important! – 重要提示!

是啊,呵呵,“I”开头的词是有些难找,但是它同样重要,very Important!

备份,要记住备份。当你把东西搞得一团糟时,只有备份文件能救你!

J – Jauntily show the admin’s email address in error message

*抱歉,本词条有误*

K – Keep RSS ‘content thieves’ away – 防范“RSS小偷”

你一定不希望有人窃取你网站的内容吧?他们只要有你的 RSS 地址就能复制内容。如果你有对方网站的IP地址(怎么获取?很简单,google 它),就能屏蔽该网站对你的 RSS 的读取。如果有不止一家网站复制你的内容,你只要增加 IP 地址就行了。

1
2
3
RewriteEngine on
RewriteCond %{REMOTE_ADDR}^69.16.226.12
RewriteRule ^(.*)$ http://newfeedurl.com/feed

来源 – Seo Black Hat

L – Limiting number of simultaneous connections – 限制并发连接数

如果你要限制并发连接数(就是同时访问你的网站的人数),就使用下面这个代码。

1 MaxClients < number-of-connections>

M – Maintenance – 制作临时维护页面

无论什么原因,可能是维护、更新,你都可能会要暂时停止你的网站。这时你就需要一个维护页。无论访客访问的是你网站的任一 URL 还是 IP 地址,都能转向到维护页面。

1
2
3
4
RewriteEngine on
RewriteCond %{REQUEST_URI}!/maintenance.html$
RewriteCond %{REMOTE_ADDR}!^123\.123\.123\.123
RewriteRule $ /maintenance.html[R=302,L]

来源 – CatsWhoCode/Woueb.net

N – Deny no referer requests [stop spam comments!] – 拒绝垃圾留言!

这是一个比下文“S”词条更简便的防 Spam 方法。其原理是,如果留言者不公开来源,即是用机器留言的,就屏蔽之。就这么简单。

1
2
3
4
5
6
RewriteEngine On
RewriteCond %{REQUEST_METHOD} POST
RewriteCond %{REQUEST_URI} .wp-comments-post\.php*
RewriteCond %{HTTP_REFERER}!.*yourblog.com.*[OR]
RewriteCond %{HTTP_USER_AGENT}^$
RewriteRule (.*)^http://%{REMOTE_ADDR}/$ [R=301,L]

来源 – WPRecipes

O – Force files when opening to ‘save as’ – 用“另存为”方式打开文档

有时候你空间里的一些文件,比如音乐、视频文件,点开后会有软件自动打开这个文件。如果你不想自动打开,这个规则能把文件强制为另存为方式下载。

1 AddType application/octet-stream .avi .mpg .mov .pdf .xls .mp4

来源 – AskApache

P – Protect your .htaccess file – 保护 .htaccess 文件

如果你的博客已经做好的十足的防护措施,是不是也要考虑一下保护你的 .htaccess 文件呢?黑客也可能通过这个文件发起攻击。下面这个规则能禁止下载 .htaccess 文件。

1
2
3
4
5
6
# STRONG HTACCESS PROTECTION
\.([Hh][Tt][Aa])”>
order allow,deny
deny from all
satisfy all

来源 – Perishable Press

Q – Quicken your site’s loading time by caching – 设置缓存

下面这个链接有详细的教程,教你如何给 WordPress 博客设置缓存。

链接 – Samaxes

R – Redirect to other pages on your site – 301重定向

1 RedirectMatch 301^/blog/.*$ http://domain.tld/target.html

来源 – Perishable Press

S – Spam! – 阻止垃圾留言

通过 .htaccess 来禁止 Spam 是个好办法。下面这个链接给出了一个 Spam 黑名单,复制到你的 .htaccess 里,你就能防止绝大部分的垃圾留言。

来源 – perishablepress.com

T – Set the Timezone of the server – 设置服务器时区

译者:代码给出的是把TZ值设置为美国时间的代码,如果服务器在中国,你可以改为Asia/Shanghai。

1 SetEnv TZ America/Indianapolis

来源 – AskApache

U – Remove /category/ from your category URL – 简化 WP 分类页面地址

WordPress 的分类页面地址里有一个 /category/ ,会不会觉得这很多余呢?下面这个代码就能去掉它!

1 RedirectMatch 301^/category/(.+)$ http://www.askapache.com/$1

或者

1 RewriteRule ^category/(.+)$ http://www.askapache.com/$1 [R=301,L]

来源 – AskApache

V – Valiantly automatically fix URL spelling mistakes – 修复错误地址

这个代码能自动检查英文地址的拼写错误。

1
2
3

CheckSpelling On

来源 – Vortex Mind

W – Redirect from http://Www.whatever to http://Whatever – 去掉网址前面的www.

使用301转跳,实现网址标准化。

1
2
3
4
5
# permanently redirect from www domain to non-www domain
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST}^www\.domain\.tld$ [NC]
RewriteRule ^(.*)$ http://domain.tld/$1 [R=301,L]

来源 – Stupid htaccess tricks

X – Make your wp-login.php page Xenophobic – 限制他人访问 wp-login

Xenophobic: “排外,仇视陌生人”

我觉得你应该适当使用这个规则。如果你不是多人博客,可以限制他人访问登录页面,以达到更高的安全性。

1
2
3
4
5

Order deny,allow
Deny from All
Allow from 123.456.789.0

来源 – Reaper-X

Y – Easily rename your .htaccess file – 重命名 .htaccess 文件

如果你的服务器不接受以句点开头的文件怎么办?重命名它呗!下面这个代码能实现 .htaccess 文件的重命名。

1
2
# rename htaccess files
AccessFileName ht.access

来源 – Perishable Press

Z – Say Zygote in your .htaccess file – 在 .htaccess 里写注释

你可能会想在 .htaccess 文件里做些解释,好让别人能看懂。那怎么做注释呢?请看下面的代码:

1 # see -thisis a comment - you can only use letters and numbers and - and _ That is why there are no commas

要注意的是注释里只能写字母、数字与下划线

译者后记:原文的作者看样子也是经过收集才写成这篇文章的。如果你看了原文,请你注意,原文的有些代码里有一些多余或错误代码。我在本译文里都已做了修正。

WordPress优化之设置浏览器端缓存:Leverage browser caching

网站上的静态内容,诸如:图片、CSS、JS、flash等,一般变动很小,甚至几个月都不会有所更改,这些体积庞大的静态内容占据了大部分网页加载时间。如何降低静态内容加载时间,甚至不需要(二次)加载静态内容,Leverage browser caching(浏览器端缓存)可以有效地解决这一问题,他使得浏览器可以缓存静态内容,使得浏览器不需要二次加载这些内容。

实现方法很简单,向.htaccess文件中添加以下代码:

#Expire Header
ExpiresActive on
ExpiresByType text/css "access 1 month"
ExpiresByType text/plain "access 2 days"
ExpiresByType text/html  "access 2 days"
ExpiresByType application/javascript "access 1 month"
ExpiresByType image/jpeg "access 1 month"
ExpiresByType image/x-icon "access 1 month"
ExpiresByType image/gif "access 1 month"
ExpiresByType image/png "access 1 month"
ExpiresByType image/ico "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresDefault "access 1 month"

#Expire Header
ExpiresActive on //开启缓存
ExpiresByType text/css “access 1 month”//css文件缓存1个月
ExpiresByType text/plain “access 2 days”//纯文本内容缓存2天
ExpiresByType text/html “access 2 days”//html文件缓存2天
ExpiresByType application/javascript “access 1 month”//JS文件缓存1个月
ExpiresByType image/jpeg “access 1 month”//jpeg图片缓存1个月
ExpiresByType image/x-icon “access 1 month”//icon缓存1个月
ExpiresByType image/gif “access 1 month”//gif图片缓存一个月
ExpiresByType image/png “access 1 month”//png图片缓存一个月
ExpiresByType image/ico “access 1 month”//ico缓存1个月
ExpiresByType application/pdf “access 1 month”//pdf文件缓存一个月
ExpiresByType application/x-shockwave-flash “access 1 month”//flash缓存一个月
ExpiresDefault “access 1 month”// 默认(未提及的可缓存文件)缓存1个月

 

保存后刷新网页,用Yslow测试可以看到,绝大部分内容都被缓存了。也就是说:读者第一次打开网页加载所有静态文件,以后再访问该页面,就不需要加载了。如果其他页面也使用相同的静态文件(比如js和css),那么访问其他页面也会很快(直接在浏览器的缓存里提取相应的静态内容)。

读者第一次打开网页加载静态文件并缓存,以后再访问该页面,就不需要加载

该方法需要apache的mod_expires,Ubuntu下用a2enmod expires开启。

分析特征码修改技术

如果你想学习免杀技术:
1.基础的汇编语言
2.修改工具(不指那些傻瓜式软件)。如:
OllyDbg . PEditor. C32ASM . MYCCL复合特征码定位器。UE .OC. 资源编辑器等。还有一些查壳 脱壳软件(如:PEID RL脱壳机等) . 以下是常用的几种免杀方法及工具:
一、要使一个木马免杀
首先要准备一个不加壳的木马,这点非常重要,否则 免杀操作就不能进行下去。 然后我们要木马的内存免杀,从上面分析可以看出,目前的内存查杀,只有瑞星最强,其它杀毒软件内存查杀现在还不起作用所以我们只针对瑞星的内存查杀,要进行内存特征码的定位和修改,才能内存免杀。
二、对符其它的杀毒软件
比如江民,金山,诺顿,卡巴。我们可以采用下面的方法,或这些方面的组合使用。
1>.入口点加1免杀法。
2>.变化入口地址免杀法
3>.加花指令法免杀法
4>.加壳或加伪装壳免杀法。
5>.打乱壳的头文件免杀法。
6>.修改文件特征码免杀法。
第三部分:免杀技术实例演示部分
一、入口点加1免杀法:
1.用到工具:PEditor
2.特点:非常简单实用,但有时还会被卡巴查杀。
3.操作要点:用PEditor打开无壳木马程序,把原入口点加1即可。
二、变化入口地址免杀法:
1.用到工具:OllyDbg,PEditor
2.特点:操作也比较容易,而且免杀效果比入口点加1点要佳。
3.操作要点:用OD载入无壳的木马程序,把入口点的前二句移到零区域去执行,然后又跳回到入口点的下面第三句继续执行。最后用PEditor把入口点改成零区域的地址。
三、加花指令法免杀法:
1.用到工具:OllyDbg,PEditor
2.特点:免杀通用性非常好,加了花指令后,就基本达到大量杀毒软件的免杀。
3.操作要点:用OD打开无壳的木马程序,找到零区域,把我们准备好的花指令填进去填好后又跳回到入口点,保存好后,再用PEditor把入口点改成零区域处填入花指令的着地址。
四、加壳或加伪装壳免杀法:
1.用到工具:一些冷门壳,或加伪装壳的工具,比如木马彩衣等。
2.特点:操作简单化,但免杀的时间不长,可能很快被杀,也很难躲过卡巴的追杀。
3.操作要点:为了达到更好的免杀效果可采用多重加壳,或加了壳后在加伪装壳的免杀效果更佳。
五、打乱壳的头文件或壳中加花免杀法:
1.用到工具:秘密行动 ,UPX加壳工具。
2.特点:操作也是傻瓜化,免杀效果也正当不错,特别对卡巴的免杀效果非常好。
3.操作要点:首先一定要把没加过壳的木马程序用UPX加层壳,然后用秘密行动这款工具中的SCramble功能进行把UPX壳的头文件打乱,从而达到免杀效果。
六、修改文件特征码免杀法:
1.用到工具:特征码定位器,OllyDbg
2.特点:操作较复杂,要定位修改一系列过程,而且只针对每种杀毒软件的免杀,要达到多种杀毒软件的免杀,必需修改各种杀毒软件的特征码。但免杀效果好。
3.操作要点:对某种杀毒软件的特征码的定位到修改一系列慢长过程。
第四部分:快速定位与修改瑞星内存特征码
一、瑞星内存特征码特点:由于技术原因,目前瑞星的内存特征码在90%以上把字符串作为病毒特征码,这样对我们的定位和修改带来了方便。
二定位与修改要点:
1>.首先用特征码定位器大致定位出瑞星内存特征码位置
2>.然后用UE打开,找到这个大致位置,看看,哪些方面对应的是字符串,用0替换后再用内存查杀进行查杀。直到找到内存特征码后,只要把字符串的大小写互换就能达到内存免杀效果。
第五部分:木马免杀综合方案
修改内存特征码——>1>入口点加1免杀法——> 1>加压缩壳——>1>再加壳或多重加壳
2>变化入口地址免杀法 2>加成僻壳 2>加壳的伪装。
3>加花指令法免杀法 3>打乱壳的头文件
4>修改文件特征码免杀法
注:这个方案可以任意组合各种不同的免杀方案。并达到各种不同的免杀效果。
第六部分:免杀方案实例演示部分
1.完全免杀方案一:
内存特征码修改 + 加UPX壳 + 秘密行动工具打乱UPX壳的头文件。
2.完全免杀方案二:
内存特征码修改 + 加压缩壳 + 加壳的伪装 )
3.完全免杀方案三:
GD内存特征码修改 + 修改各种杀毒软件的文件特征码 + 加压缩壳
4.完全免杀方案四:
内存特征码修改 + 加花指令 + 加压壳
5.完全变态免杀方案五:
内存特征码修改 + 加花指令 + 入口点加1 + 加压缩壳UPX + 打乱壳的头文件
还有其它免杀方案可根据第五部分任意组合

修复数据库中被SyntaxHighlighter替换的特殊符号

< > " & '修改自http://quartergeek.com/fix-special-char-replaced-by-syntaxhighlighter

代码增加了单引号的替换,同时增加几个变量提醒用户修改。

自从换上了Crayon Syntax Highlighter,发现原来<>&”’符号被WP替换成了<  > " & ‘而Crayon Syntax Highlighter又不能转换,只好转回来,于是写了下面这个脚本,成功地替换回来了。

set_charset("utf8");
$result = $conn->query("SELECT post_content, ID FROM ".$ta);
$stmt = $conn->prepare("UPDATE ".$ta." SET post_content = ? WHERE ID = ?"); 

$search  = array("<", ">", """, "&", "'");
$replace = array("<"   , ">"   , "\""    , "&"    ,"'");
while ($row = $result->fetch_array())
{
  $id = $row['ID'];
  $post_content = str_replace($search, $replace, $row['post_content']);
  $stmt->bind_param('si', $post_content, $id);
  if (!$stmt->execute())
  {
    die("\n[ERROR!]".$id."\n");
  }
  else
  {
    echo "[Success]".$id."\n";
  }
}

$result->free();
$conn->close();
?>

 

Setting up awstats with apache 2 & geoip on debian-ubuntu

Awstats is a web log analyzer. It gives really useful statistics about traffic on your web sites.This log analyzer works as aCGI or from command line and shows you all possible information your log contains, in few graphical web Awstats Monthly Viewpages.

yesterday, it took me many hour to configure it on my server, I took help from many blogs to complete it.

Hope this can help someone.

1. Installing Awstats:

ab@web1:~$ sudo apt-get install awstats

This command will install awstats.Now we have to configure Apache.

2. Configuring Apache2:

Now make the following changes inawstats.conf file.

ab@web1:~$ sudo vi /etc/apache2/awstats.conf

Alias /awstatsclasses “/usr/share/awstats/lib/”
Alias /awstats-icon/ “/usr/share/awstats/icon/”
Alias /awstatscss “/usr/share/doc/awstats/examples/css”
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
ScriptAlias /awstats/ /usr/lib/cgi-bin/
Options ExecCGI -MultiViews +SymLinksIfOwnerMatch

After, Making these changes, we have to include this file (/etc/apache2/awstats.conf) in to our apache.conf file, so we can access it over http.

ab@web1:~$ sudo vi /etc/apache2/apache2.conf

Include /etc/apache2/awstats.conf

Now, reload the apache.

ab@web1:~$  sudo /etc/init.d/apache2 reload

Now you can try to open http://www.mysite.com/awstats/awstats.pl but you will get this error:  Error: SiteDomain parameter not defined in your config/domain file. You must edit it for using this version of AWStats.

That means we have to configure Awstats.

3. Configuring Awstats:

By default awstats provides a default configuration file named /etc/awstats/awstats.conf. Copy this file to /etc/awstats/awstats.www.mysite.com.conf:

~$sudo cp /etc/awstats/awstats.conf /etc/awstats/awstats.www.mysite.com.conf

and edit the file:

~$sudo vi /etc/awstats/awstats.www.mysite.com.conf

and check for the following lines and edit those for your needs:

LogFile=”/var/log/apache2/access.log”
SiteDomain=”mysite.com”

This is all setted up, now you need to generate the first stats.

4. Generating the First Stats:

in order to generate the first stats, you need to call the script as root using the following command line:

~:$sudo -u www-data /usr/bin/perl /usr/lib/cgi-bin/awstats.pl -update -config=www.mysite.com

After finishing this you can try to open http://www.mysite.com/awstats/awstats.pl this will show you the stats.

Now for automatic update the stats you need to put the following command in your cron.

0 2 * * * www-data [ -x /usr/lib/cgi-bin/awstats.pl -a -f /etc/awstats/awstats.conf -a -r /var/log/apache/access.log ] && /usr/lib/cgi-bin/awstats.pl -config=mysite.com -update >/dev/null

GeoIP Information for AWStats

Improve country and city location information in your web analytics reports

Awstats showing country info

AWStats offers three different ways to provide Country level information about visitors (“hosts”) connecting to your site. By default, the host domain extension is used if you have performed reverse DNS look-up. While this results in human readable host names as well, reverse DNS lookup is time consuming. Alternatively, GeoIP plugins can be used. They match the host IP with a database of IP allocations which is already installed locally on your computer, significantly reducing the time needed to match a host to a location. There are two country level GeoIP databases available. AGeo::IPFree Perl module is available, but it does not appear that the database is being maintained. The better solution is MaxMind’s Geo::IP GeoLite Country open source version, said to be 97% accurate.

Download the current GeoLite Country, GeoLite City and Organization (AS Numbers)databases
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
wget http://geolite.maxmind.com/download/geoip/database/asnum/GeoIPASNum.dat.gz

Move data files to directory and unzip.

mv GeoIP.dat.gz /usr/local/share/GeoIP/
gunzip /usr/local/share/GeoIP/GeoIP.dat.gz
mv GeoLiteCity.dat.gz /usr/local/share/GeoIP/
gunzip /usr/local/share/GeoIP/GeoLiteCity.dat.gz
mv GeoIPASNum.dat.gz /usr/local/share/GeoIP/
gunzip /usr/local/share/GeoIP/GeoIPASNum.dat.gz
Install the GeoIP program.

1. wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP-1.4.5.tar.gz
tar -xvzf GeoIP-1.4.5.tar.gz
cd GeoIP-1.4.5
./configure
make
make check
sudo make install

2.wget http://geolite.maxmind.com/download/geoip/api/perl/Geo-IP-1.35.tar.gz
tar -xvzf Geo-IP-1.35.tar.gz
cd Geo-IP-1.35
perl Makefile.PL LIBS=’-L/usr/local/lib’
make
make test
sudo make install

Update your awstats.mysite.conf configuration file

Look for the following geoip LoadPlugin options in your AWStats configuration file. Update them with the full path to each of the two database files.

LoadPlugin=”geoip GEOIP_STANDARD /usr/local/share/GeoIP/GeoIP.dat”
LoadPlugin=”geoip_city_maxmind GEOIP_STANDARD /usr/local/share/GeoIP/GeoLiteCity.dat”
LoadPlugin=”geoip_org_maxmind GEOIP_STANDARD /usr/local/share/GeoIP/GeoIPASNum.dat”

Well, It’s done and now you can check the stats with country info.

Cheers!!

DH的虚拟主机上是有GeoIP.dat的,可以这样找到路径:

$ locate GeoIP.dat
/usr/local/share/GeoIP/GeoIP.dat
/usr/share/GeoIP/GeoIP.dat

安装库模块:

perl -MCPAN -e ‘install “Geo::IP”‘

perl -MCPAN -e ‘install “Geo::IP::PurePerl”‘

我找到两个,比了一下时间,/usr/local那个要更新一点,所以用这个。所以,像下边这样修改common.conf:

找到
# LoadPlugin=”geoip GEOIP_STANDARD /pathto/GeoIP.dat”
取消注释,改为
LoadPlugin=”geoip GEOIP_STANDARD /usr/local/share/GeoIP/GeoIP.dat”

这样做,就可以开启GeoIP功能了,也就是在IP栏的右边,显示出IP所在国家。

如果,你认为DH的GeoIP.dat数据比较老,想使用比较新的,可以直接从MaxMind下载。可以下载GeoIP.dat.gzGeoLiteCity.dat.gz,之后解压,放在某处。假设放在/home/yourname/awstats/目录下。

LoadPlugin=”geoip GEOIP_STANDARD /pathto/GeoIP.dat”
LoadPlugin=”geoip_city_maxmind GEOIP_STANDARD /pathto/GeoIPCity.dat”
把以上两句改为:
LoadPlugin=”geoip GEOIP_STANDARD /home/awstats/awstats/GeoIP.dat”
LoadPlugin=”geoip_city_maxmind GEOIP_STANDARD /home/yourname/awstats/GeoIPCity.dat”

这样,就可以有国家和城市两栏显示了。

但是,这个城市显示比较粗糙一点,还可以利用QQ的纯真库IP信息来显示,有人做了一个这样的插件,qqhostinfo。需要以下步骤:

1. 下载纯真版IP数据库,解压缩后,只使用QQWry.dat
2. 下载qqhostinof.pm
3. 下载分析QQWry.dat的脚本
4. 把以上文件都放在cgi-bin/plugins下
5. 修改qqwry.pl,把./QQWry.dat改为${DIR}/plugins/QQWry.Dat
6. 修改common.conf,加入LoadPlugin=”qqhostinfo”

经过以上设置就可以了,会在GeoIPCity右边出现Location栏,里边有很具体的地址信息。

在使用qqhostinfo的时候,需要安装Net::XWhois模块。发现DH的主机上已经装了这个模块。

如果在没有的虚拟主机上安装,由于没有管理员权限,所以,正常的安装过程在make install的时候会报错的。所以,可以这样:

$ make Makefile.PL PREFIX=/home/yourname/awstats/lib # 这样可以把最后的安装都放在这个目录里
$ make
$ make install

另外,如果对中文搜索引擎统计有更高需求的话,可以使用车东的TOP 8 Chinese local search engines。这东西是将近一年前的,适用于AWStats-6.6,对于6.7来说不太合适。我做了一个6.7的search_engines.pm.patch,可以对search_engines.pm打patch。不管在本地,还是在DH主机的shell里,可以

patch search_engines.pm search_engines.pm.patch

 

1.更新若干有用的模块:      

       ppm install Geo::IP::PurePerl
       ppm install Geography::Countries
       ppm install IP::Country 
       ppm install Geo-IPfree
       ppm install  Net-Xwhois 

2.GeoIP和qqhostinfo需要下载相关文件

GeoIP需要GeoIP.dat;qqhostinfo需要qqhostinfo.pm、qqwry.pl、QQWry.dat3个文件,把他们都放到cgi-bin的插件目录里面.

GeoIP.dat,qqhostinfo.pm、qqwry.pl、QQWry.dat3 打包CSDN下载:
http://download.csdn.net/detail/debugcs/4360307

 

3、插件的开启

        开启 GeoIP 反查模块
       LoadPlugin=”geoip GEOIP_STANDARD C:/Perl/lib/GeoIP.dat”
       开启 IP 反查的外挂
       LoadPlugin=”geoipfree”
  
       # 开启 decodeutfkeys 外挂,解决“用以搜索的关键词”乱码
       LoadPlugin=”decodeutfkeys”    
     
       设定IIS的log时间
       LoadPlugin=”timezone +8″好了,所以的设置都弄好了,awstats已经能够很好的工作了。
       # Plugin: qqhostinfo,显示访问者地区,下面的语句需要手工添加。
       LoadPlugin=”qqhostinfo” 

       # 开启HostInfo,这个是qqhostinfo插件的前提

 

       LoadPlugin=”hostinfo”
*  上面插件加载地址按需修改 

日志统计页面GeoIP显示效果:

It may not be able to give the same level of detail as a commercial package such as Urchin, but it provides more than enough detail for most purposes.
On Ubuntu you can install awstats via apt:
apt-get install awstats
This will install the base package however, if you want to lever its full power you will need to install a couple of extra Perl modules.
apt-get install libnet-ip-perl
If you would like to see which countries your visitors are based in then you should look at one of the IP to Country plugins which will give you a Geo IP lookup.
You can choose between either geoipfree or GeoIp.
Other useful plugins are:

  • Plugin: HostInfo – which gives you a popup whois window
  • Plugin: IPv6 – adds IPv6 support
  • Plugin: Tooltips – confused by all the information? This will take away the mystery :)

On rpm-based system there is a handy little perl script that walks you through the initial configuration. On Ubuntu you have to do it by hand, however most of the configuration is self-explanatory.
Simply copy the provided sample config to a new config file:
cp awstats.conf awstats.yoursite.tld.conf
NB: You will need a separate configuration file for each vhost you wish to examine.
Edit the conf file to match your site’s setup. Important things to pay attention to are any domain aliases, the location of the log files and any IPs you wish to ignore (eg. your own)
You can also decide which optional plugins you want to load. Simply add a line to invoke each one:
LoadPlugin="geoipfree"
LoadPlugin="ipv6"
LoadPlugin="Tooltips"

geoipfree requires an extra perl module. On ubuntu the package is called libgeo-ipfree-perl
You are almost ready!
You now need to configure Apache to allow access to your awstats pages.
Rather than messing about with my main Apache config file I found it neater to place the awstats specific information in an external file called awstats.conf:

Options None
AllowOverride None
Order allow,deny
Allow from all

Alias /awstatsicons/ /usr/share/awstats/icon/
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

AllowOverride None
Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all


Then use an include directive in your main Apache config (apache2.conf):
Include /etc/apache2/awstats.conf
You then need to add it in to your vhost:
ScriptAlias /whereyouwanttoaccessit/ /usr/lib/cgi-bin/
If you navigate to:

http://www.yourdomain.tld/whereyouwanttoaccessit/awstats.pl?config=yourdomain.tld

You *should* be able to access your stats.
Of course you’ll need to actually generate the stats first:
/usr/lib/cgi-bin/awstats.pl -config=yourdomain.tld -update
I would recommend you run this from the command line once to create the first set of stats and then add a cronjob:
crontab -e
0,10,20,30,40,50 * * * * /usr/lib/cgi-bin/awstats.pl -config=yourdomain.tld -update >/dev/null
wq

This will update your stats every 10 minutes, which means that you’ll only have to update a few lines at a time. If your site is busy this makes sense :)
The last part of the line is important:
>/dev/null
By default cron emails the user each time it runs. You really don’t want 6 emails an hour telling you that Awstats has run :)

How to reset awstats

Just delete the files in /var/lib/awtstats (directory defined by DirData parameter)

sudo su - root
cd /var/lib/awstats
rm *
logout

Notes

  • you have to be root because for some reason, sudo rm -R awstats/* wouldn’t work.
  • if you get the message Never updated (See 'Build/Update' on awstats_setup.html page) whereas the awstats files are there in /var/lib/awtstats, it’s probably a permission problem on this folder.

EXCEL、WORD、POWERPOINT+VBA利用CDO组件发送邮件

前几天学习宏病毒的时候用到了,顺手记录一下~
Sub EMAIL()
Dim cm As Variant
Set cm = CreateObject(“CDO.Message”) ‘创建对象
cm.From = “” ‘设置发信人的邮箱
cm.To = “” ‘设置收信人的邮箱
cm.Subject = “主题:邮件发送试验” ‘设定邮件的主题
‘cm.TextBody = “宝坻一中信息中心/” ‘使用文本格式发送邮件
cm.HtmlBody = “邮件发送试验^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^” ‘使用html格式发送邮件
‘cm.AddAttachment Server.MapPath(“test.zip”)
‘发送附件test.zip。
stUl = “http://schemas.microsoft.com/cdo/configuration/” ‘微软服务器网址
With cm.Configuration.Fields
.Item(stUl & “smtpserver”) = “smtp.bdyz.net” ‘SMTP服务器地址
.Item(stUl & “smtpserverport”) = 25 ‘SMTP服务器端口
.Item(stUl & “sendusing”) = 2 ‘发送端口
.Item(stUl & “smtpauthenticate”) = 1 ‘
.Item(stUl & “sendusername”) = “changxi” ‘发送方邮箱名称
.Item(stUl & “sendpassword”) = “*************” ‘发送方邮箱密码
.Update
End With

cm.Send ‘最后当然是执行发送了
Set cm = Nothing
‘发送成功后即时释放对象
End Sub

修改WORDPRESS模版底部信息

默认安装完毕wordpress后,需要完善的地方很有很多,首选就是选择一个符合网站风格的模版,面对众多免费worpress模版,肯定会挑花了你眼。这真是worpress强大之初,所有你想到或没想到的,wp团队都为你想到和做到, 你只需要掌握使用worpress的方法就可以了。

这里教大家修改wp模版的底部信息,将“自豪地采用wordpress”修改自己想要的结果。作为实例,我将“我自豪地采用wordpress”修改为“傲娇地采用wordpress”,其url链接地址为:maoxian.de

 

将“自豪地采用wordpress”修改为“傲娇地采用wordpress”

打开文件:./wp-content/languages/zh_CN.po,搜索“自豪地采用”并修改为相对应的信息就可以了。

接着修改模版文件下面的footer.php文件即可。

打开/wp-content/themes/twentyten/footer.php文件,将http://wordpress.org/修改为maoxian.de即可

在PPT和Word中添加带有语法高亮的代码块

问题很简单,我想在ppt中添加带有语法高亮的程序代码,搞了几天,总算找到了比较好的解决方法了。

详细的要求如下:

1.代码带有语法高亮。

2.结果是矢量图,也就是说可以随便缩小放大的,因此别拿直接对代码截图来忽悠我。

3.最好带有行号。

 

方法一:

下载软件SciTE,下载地址:http://www.fauskes.net/nb/syntaxms/

这个软件是我从网上搜到的,外国人写的。效果如下:

0_1318335729CRmh

不过要把带有语法高亮的格式拷贝到ppt里有点麻烦,把代码复制到这个软件内,然后用Copy as RTF复制,再粘到Word里面,然后再在ppt里面选择性粘贴为带格式文本。

当然这种方法不满足条件3。

PPT里的效果如下:

0_13183357924zyj

好吧,其实我想说一开始我看到大段的英文就很头疼,我简单看了下,没有看到在ppt里面需要额外的步骤,直接粘贴了,结果当然很失望,于是继续找其他的方法。

 

 

以下两个应该是我自己原创的吧。

方法二:

首先使用notepad++打印源代码的话可以获得带有完整语法高亮的代码,我一般打印成pdf,但是pdf在PPT里面虽然能够直接插入,但是pdf一般都是一整页A4,如果代码比较短的话需要对pdf重新进行裁剪,还是比较麻烦的。

这个方法不满足条件2,虽然在pdf里面内容是矢量的,但是我发现在PowerPoint里面插入pdf后放大时出现了模糊。

 

方法三:

继续想,本质上我想要一个矢量图,所以我就想到了eps格式,把pdf另存为eps后就直接能够在PPT里面像插入图片一样插入代码了。编辑是也可以直接像编辑图片一样,比较方便,可以随意裁剪。

这种方法基本能够满足上述三个条件了。

效果如下:

0_1318335993rYrE

 

最后总结一下,整体的思路分为两个方向:

a)将代码自动转换成带有格式的文本。方法一是这个路子,另外一个流产的方法是生成html代码然后插入到PPT里,但是这个我没有试成功。

b)将代码转换成带有语法高亮的矢量图。方法二、三都是这个思路,虽然方法二在实际插入到PPT中时失去了矢量图的特性。而将代码转换成带语法高亮的图片则是依靠Notepad++强大的打印功能。

 

个人推荐方法三,因为我不是很喜欢SciTE里的语法高亮,而Notepad++提供了各种语法高亮方案,可以自定义输出,并且可以输出行号。

 

 

10月18日更新

找到了一个比SciTE更加强大的软件HightLight,官网是:http://www.andre-simon.de/

这款软件可以把源码输出成各种不同格式,例如tex、html等。我最初就是想找在Latex下的代码高亮而找到的。

0_1318930764lQin

就看这个界面就知道非常强大了……支持自动调整格式、添加行号、n多配色方案,字体设置等等……

 

不过需要注意的是,上述软件均对中文支持不好!!!代码有中文注释的话一律会被转换成乱码。建议转换前去掉注释或者干错习惯写英文注释。

VIM查看文件编码文件编码格式转换文件名编码转换

如果你需要在Linux中操作windows下的文件,那么你可能会经常遇到文件编码转换的问题。Windows中默认的文件格式是GBK(gb2312),而Linux一般都是UTF-8。下面介绍一下,在Linux中如何查看文件的编码及如何进行对文件进行编码转换。

查看文件编码
在Linux中查看文件编码可以通过以下几种方式:
1.在Vim中可以直接查看文件编码
:set fileencoding
即可显示文件编码格式。
如果你只是想查看其它编码格式的文件或者想解决用Vim查看文件乱码的问题,那么你可以在
~/.vimrc 文件中添加以下内容:

set encoding=utf-8 fileencodings=ucs-bom,utf-8,cp936

这样,就可以让vim自动识别文件编码(可以自动识别UTF-8或者GBK编码的文件),其实就是依照fileencodings提供的编码列表尝试,如果没有找到合适的编码,就用latin-1(ASCII)编码打开。

文件编码转换
1.在Vim中直接进行转换文件编码,比如将一个文件转换成utf-8格式
:set fileencoding=utf-8

2. iconv 转换,iconv的命令格式如下:
iconv -f encoding -t encoding inputfile
比如将一个UTF-8 编码的文件转换成GBK编码
iconv -f GBK -t UTF-8 file1 -o file2

文件名编码转换:

从Linux往 windows拷贝文件或者从windows往Linux拷贝文件,有时会出现中文文件名乱码的情况,出现这种问题的原因是因为,windows的文件名中文编码默认为GBK,而Linux中默认文件名编码为UTF8,由于编码不一致,所以导致了文件名乱码的问题,解决这个问题需要对文件名进行转码。

在Linux中专门提供了一种工具convmv进行文件名编码的转换,可以将文件名从GBK转换成UTF-8编码,或者从UTF-8转换到GBK。

首先看一下你的系统上是否安装了convmv,如果没安装的话用:
yum -y install convmv
安装。

下面看一下convmv的具体用法:

convmv -f 源编码 -t 新编码 [选项] 文件名

常用参数:
-r 递归处理子文件夹
–notest 真正进行操作,请注意在默认情况下是不对文件进行真实操作的,而只是试验。
–list 显示所有支持的编码
–unescap 可以做一下转义,比如把%20变成空格
比如我们有一个utf8编码的文件名,转换成GBK编码,命令如下:

convmv -f UTF-8 -t GBK –notest utf8编码的文件名

这样转换以后”utf8编码的文件名”会被转换成GBK编码(只是文件名编码的转换,文件内容不会发生变化)

vim 编码方式的设置

和所有的流行文本编辑器一样,Vim 可以很好的编辑各种字符编码的文件,这当然包括UCS-2、UTF-8 等流行的 Unicode 编码方式。然而不幸的是,和很多来自 Linux 世界的软件一样,这需要你自己动手设置。

Vim 有四个跟字符编码方式有关的选项,encoding、fileencoding、fileencodings、termencoding (这些选项可能的取值请参考 Vim 在线帮助 :help encoding-names),它们的意义如下:

* encoding: Vim 内部使用的字符编码方式,包括 Vim 的 buffer (缓冲区)、菜单文本、消息文本等。默认是根据你的locale选择.用户手册上建议只在 .vimrc 中改变它的值,事实上似乎也只有在.vimrc 中改变它的值才有意义。你可以用另外一种编码来编辑和保存文件,如你的vim的encoding为utf-8,所编辑的文件采用cp936编码,vim会自动将读入的文件转成utf-8(vim的能读懂的方式),而当你写入文件时,又会自动转回成cp936(文件的保存编码).

* fileencoding: Vim 中当前编辑的文件的字符编码方式,Vim 保存文件时也会将文件保存为这种字符编码方式 (不管是否新文件都如此)。

* fileencodings: Vim自动探测fileencoding的顺序列表, 启动时会按照它所列出的字符编码方式逐一探测即将打开的文件的字符编码方式,并且将 fileencoding 设置为最终探测到的字符编码方式。因此最好将Unicode 编码方式放到这个列表的最前面,将拉丁语系编码方式 latin1 放到最后面。

* termencoding: Vim 所工作的终端 (或者 Windows 的 Console 窗口) 的字符编码方式。如果vim所在的term与vim编码相同,则无需设置。如其不然,你可以用vim的termencoding选项将自动转换成term的编码.这个选项在 Windows 下对我们常用的 GUI 模式的 gVim 无效,而对 Console 模式的Vim 而言就是 Windows 控制台的代码页,并且通常我们不需要改变它。

好了,解释完了这一堆容易让新手犯糊涂的参数,我们来看看 Vim 的多字符编码方式支持是如何工作的。

1. Vim 启动,根据 .vimrc 中设置的 encoding 的值来设置 buffer、菜单文本、消息文的字符编码方式。

2. 读取需要编辑的文件,根据 fileencodings 中列出的字符编码方式逐一探测该文件编码方式。并设置 fileencoding 为探测到的,看起来是正确的 (注1) 字符编码方式。

3. 对比 fileencoding 和 encoding 的值,若不同则调用 iconv 将文件内容转换为encoding 所描述的字符编码方式,并且把转换后的内容放到为此文件开辟的 buffer 里,此时我们就可以开始编辑这个文件了。注意,完成这一步动作需要调用外部的 iconv.dll(注2),你需要保证这个文件存在于 $VIMRUNTIME 或者其他列在 PATH 环境变量中的目录里。

4. 编辑完成后保存文件时,再次对比 fileencoding 和 encoding 的值。若不同,再次调用 iconv 将即将保存的 buffer 中的文本转换为 fileencoding 所描述的字符编码方式,并保存到指定的文件中。同样,这需要调用 iconv.dll由于 Unicode 能够包含几乎所有的语言的字符,而且 Unicode 的 UTF-8 编码方式又是非常具有性价比的编码方式 (空间消耗比 UCS-2 小),因此建议 encoding 的值设置为utf-8。这么做的另一个理由是 encoding 设置为 utf-8 时,Vim 自动探测文件的编码方式会更准确 (或许这个理由才是主要的 ;)。我们在中文 Windows 里编辑的文件,为了兼顾与其他软件的兼容性,文件编码还是设置为 GB2312/GBK 比较合适,因此 fileencoding 建议设置为 chinese (chinese 是个别名,在 Unix 里表示 gb2312,在 Windows 里表示cp936,也就是 GBK 的代码页)。