站长常用代码,一定要收藏
.htaccess 配置
301重定向
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.pandaidea.com$ [NC]
RewriteRule ^(.*)$ http://www.pandaidea.com/$1 [L,R=301]
自定义404页面
ErrorDocument 404 /404.html
注意页面内容长度必须大于512字节,否则在IE中只能显示默认404页面
目录内禁止执行PHP
<Files ~ “.php”>
Order allow,deny
Deny from all
</Files>
禁止某个IP/段访问
Deny from 60.190.17.*
Rewrite规则 for wordpress
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
启用 GZip 压缩
确保LoadModule deflate_module modules/mod_deflate.so前的井号键已经删除,并且在httpd.conf或者.htaccess中添加以下代码:
<ifmodule mod_deflate.c>
DeflateCompressionLevel 9
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/x-httpd-php
AddOutputFilter DEFLATE js css
</ifmodule>
在线检测是否启用gzip:http://httpcode.pandaidea.com/
PHP代码
301重定向
<?php
$url=”http://www.pandaidea.com”.$_SERVER[“REQUEST_URI”];
header(“HTTP/1.1 301 Moved Permanently”);
header (“Location:$url”);
503 服务暂时不可用
<?php
header(‘HTTP/1.1 503 Service Temporarily Unavailable’);
header(‘Status: 503 Service Temporarily Unavailable’);
备案等需要临时不能打开网站时使用
未建好页面应返回503状态码,防止被删除:503返回码的含义是“Service Unavailable”,百度会认为该网页临时不可访问,通常网站临时关闭,带宽有限等会产生这种情况。对于网页返回503,百度spider不会把这条url直接删除,短期内会再访问。届时如果网页已恢复,则正常抓取;如果继续返回503,短期内还会反复访问几次。但是如果网页长期返回503,那么这个url仍会被百度认为是失效链接,从搜索结果中删除。
删除网站中所有文件的BOM头
<?php
if (isset($_GET[‘dir’])){ //设置文件目录
$basedir=$_GET[‘dir’];
}else{
$basedir = ‘.';
}
$auto = 1;
checkdir($basedir);
function checkdir($basedir){
if ($dh = opendir($basedir)) {
while (($file = readdir($dh)) !== false) {
if ($file != ‘.’ && $file != ‘..’){
if (!is_dir($basedir.”/”.$file)) {
echo “filename: $basedir/$file “.checkBOM(“$basedir/$file”).” <br>”;
}else{
$dirname = $basedir.”/”.$file;
checkdir($dirname);
}
}
}
closedir($dh);
}
}
function checkBOM ($filename) {
global $auto;
$contents = file_get_contents($filename);
$charset[1] = substr($contents, 0, 1);
$charset[2] = substr($contents, 1, 1);
$charset[3] = substr($contents, 2, 1);
if (ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191) {
if ($auto == 1) {
$rest = substr($contents, 3);
rewrite ($filename, $rest);
return (“<font color=red>BOM found, automatically removed.</font>”);
} else {
return (“<font color=red>BOM found.</font>”);
}
}
else return (“BOM Not Found.”);
}
function rewrite ($filename, $data) {
$filenum = fopen($filename, “w”);
flock($filenum, LOCK_EX);
fwrite($filenum, $data);
fclose($filenum);
}
Nginx规则
Rewrite 规则 for wordpress
if (!-e $request_filename){
rewrite (.*) /index.php;
}
robots.txt 语法
允许所有机器人
User-agent: *
Disallow:
另一种写法:
User-agent: *
Allow:/
全站禁止收录
User-agent: *
Disallow: /
禁止某个目录收录
User-agent: *
Disallow: /cgi-bin/
在网页中用HTML代码阻止robots
<meta name=”robots” content=”noindex” />
为保证robots.txt的语法正确可使用百度Robots.txt工具生成
其他代码
解除网页右键屏蔽
javascript:(function() { function R(a){ona = “on”+a;if(window.addEventListener) window.addEventListener(a, function(e) { for(var n=e.originalTarget; n; n=n.parentNode) n[ona]=null; },true); window[ona]=null; document[ona]=null; if(document.body)document.body[ona]=null; } R(“contextmenu”);R(“click”);R(“mousedown”); R(“mouseup”); R(“selectstart”);})()
把以上代码加入收藏,在要解除屏蔽的网页上点击收藏链接
禁止其他网站iframe本站页面
<script>
if(top != self){ top.location.href = self.location.href; }
</script>