站長必備的一些常用代碼
發(fā)布時間:2014-04-28
閱讀次數(shù):
文章摘要:站長必備的一些常用代碼 .htaccess配置,301重定向 RewriteEngineOn RewriteCond%{HTTP_HOST}!^yudun-sh.com$[NC] RewriteRule^(.*)$//$1[L,R=301] 自定義404頁面 ErrorDocument404/404.html 注意頁面內(nèi)容長度必須...
.htaccess配置
301重定向
RewriteEngineOn
RewriteCond%{HTTP_HOST}!^yudun-sh.com$[NC]
RewriteRule^(.*)$//$1[L,R=301]
自定義404頁面
ErrorDocument404/404.html
注意頁面內(nèi)容長度必須大于512字節(jié),否則在IE中只能顯示默認404頁面
目錄內(nèi)禁止執(zhí)行PHP
<Files~".php">
Orderallow,deny
Denyfromall
</Files>
禁止某個IP/段訪問
Denyfrom60.190.17.*
Rewrite規(guī)則forwordpress
RewriteEngineOn
RewriteBase/
RewriteRule^index\.php$-[L]
RewriteCond%{REQUEST_FILENAME}!-f
RewriteCond%{REQUEST_FILENAME}!-d
RewriteRule./index.php[L]
啟用GZip壓縮
確保LoadModuledeflate_modulemodules/mod_deflate.so前的井號鍵已經(jīng)刪除,并且在httpd.conf或者.htaccess中添加以下代碼:
<ifmodulemod_deflate.c>
DeflateCompressionLevel9
AddOutputFilterByTypeDEFLATEtext/htmltext/plaintext/xmlapplication/x-httpd-php
AddOutputFilterDEFLATEjscss
</ifmodule>
PHP代碼
301重定向
<?php
$url="/".$_SERVER["REQUEST_URI"];
header("HTTP/1.1301MovedPermanently");
header("Location:$url");
503服務(wù)暫時不可用
<?php
header('HTTP/1.1503ServiceTemporarilyUnavailable');
header('Status:503ServiceTemporarilyUnavailable');
備案等需要臨時不能打開網(wǎng)站時使用
未建好頁面應(yīng)返回503狀態(tài)碼,防止被刪除:503返回碼的含義是“ServiceUnavailable”,百度會認為該網(wǎng)頁臨時不可訪問,通常網(wǎng)站臨時關(guān)閉,帶寬有限等會產(chǎn)生這種情況。對于網(wǎng)頁返回503,百度spider不會把這條url直接刪除,短期內(nèi)會再訪問。屆時如果網(wǎng)頁已恢復(fù),則正常抓??;如果繼續(xù)返回503,短期內(nèi)還會反復(fù)訪問幾次。但是如果網(wǎng)頁長期返回503,那么這個url仍會被百度認為是失效鏈接,從搜索結(jié)果中刪除。
刪除網(wǎng)站中所有文件的BOM頭
<?php
if(isset($_GET['dir'])){//設(shè)置文件目錄
$basedir=$_GET['dir'];
}else{
$basedir='.';
}
$auto=1;
checkdir($basedir);
functioncheckdir($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);
}
}
functioncheckBOM($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("<fontcolor=red>BOMfound,automaticallyremoved.</font>");
}else{
return("<fontcolor=red>BOMfound.</font>");
}
}
elsereturn("BOMNotFound.");
}
functionrewrite($filename,$data){
$filenum=fopen($filename,"w");
flock($filenum,LOCK_EX);
fwrite($filenum,$data);
fclose($filenum);
}
Nginx規(guī)則
Rewrite規(guī)則forwordpress
if(!-e$request_filename){
rewrite(.*)/index.php;
}
robots.txt語法
允許所有機器人
User-agent:*
Disallow:
另一種寫法:
User-agent:*
Allow:/
全站禁止收錄
User-agent:*
Disallow:/
禁止某個目錄收錄
User-agent:*
Disallow:/cgi-bin/
在網(wǎng)頁中用HTML代碼阻止robots
<metaname="robots"content="noindex"/>
為保證robots.txt的語法正確可使用百度Robots.txt工具生成
其他代碼
解除網(wǎng)頁右鍵屏蔽
javascript:(function(){functionR(a){ona="on"+a;if(window.addEventListener)window.addEventListener(a,function(e){for(varn=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");})()
把以上代碼加入收藏,在要解除屏蔽的網(wǎng)頁上點擊收藏鏈接
禁止其他網(wǎng)站iframe本站頁面
<script>
if(top!=self){top.location.href=self.location.href;}
</script> .htaccess配置
301重定向
RewriteEngineOn
RewriteCond%{HTTP_HOST}!^yudun-sh.com$[NC]
RewriteRule^(.*)$//$1[L,R=301]
自定義404頁面
ErrorDocument404/404.html
注意頁面內(nèi)容長度必須大于512字節(jié),否則在IE中只能顯示默認404頁面
目錄內(nèi)禁止執(zhí)行PHP
<Files~".php">
Orderallow,deny
Denyfromall
</Files>
禁止某個IP/段訪問
Denyfrom60.190.17.*
Rewrite規(guī)則forwordpress
RewriteEngineOn
RewriteBase/
RewriteRule^index\.php$-[L]
RewriteCond%{REQUEST_FILENAME}!-f
RewriteCond%{REQUEST_FILENAME}!-d
RewriteRule./index.php[L]
啟用GZip壓縮
確保LoadModuledeflate_modulemodules/mod_deflate.so前的井號鍵已經(jīng)刪除,并且在httpd.conf或者.htaccess中添加以下代碼:
<ifmodulemod_deflate.c>
DeflateCompressionLevel9
AddOutputFilterByTypeDEFLATEtext/htmltext/plaintext/xmlapplication/x-httpd-php
AddOutputFilterDEFLATEjscss
</ifmodule>
PHP代碼
301重定向
<?php
$url="/".$_SERVER["REQUEST_URI"];
header("HTTP/1.1301MovedPermanently");
header("Location:$url");
503服務(wù)暫時不可用
<?php
header('HTTP/1.1503ServiceTemporarilyUnavailable');
header('Status:503ServiceTemporarilyUnavailable');
備案等需要臨時不能打開網(wǎng)站時使用
未建好頁面應(yīng)返回503狀態(tài)碼,防止被刪除:503返回碼的含義是“ServiceUnavailable”,百度會認為該網(wǎng)頁臨時不可訪問,通常網(wǎng)站臨時關(guān)閉,帶寬有限等會產(chǎn)生這種情況。對于網(wǎng)頁返回503,百度spider不會把這條url直接刪除,短期內(nèi)會再訪問。屆時如果網(wǎng)頁已恢復(fù),則正常抓??;如果繼續(xù)返回503,短期內(nèi)還會反復(fù)訪問幾次。但是如果網(wǎng)頁長期返回503,那么這個url仍會被百度認為是失效鏈接,從搜索結(jié)果中刪除。
刪除網(wǎng)站中所有文件的BOM頭
<?php
if(isset($_GET['dir'])){//設(shè)置文件目錄
$basedir=$_GET['dir'];
}else{
$basedir='.';
}
$auto=1;
checkdir($basedir);
functioncheckdir($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);
}
}
functioncheckBOM($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("<fontcolor=red>BOMfound,automaticallyremoved.</font>");
}else{
return("<fontcolor=red>BOMfound.</font>");
}
}
elsereturn("BOMNotFound.");
}
functionrewrite($filename,$data){
$filenum=fopen($filename,"w");
flock($filenum,LOCK_EX);
fwrite($filenum,$data);
fclose($filenum);
}
Nginx規(guī)則
Rewrite規(guī)則forwordpress
if(!-e$request_filename){
rewrite(.*)/index.php;
}
robots.txt語法
允許所有機器人
User-agent:*
Disallow:
另一種寫法:
User-agent:*
Allow:/
全站禁止收錄
User-agent:*
Disallow:/
禁止某個目錄收錄
User-agent:*
Disallow:/cgi-bin/
在網(wǎng)頁中用HTML代碼阻止robots
<metaname="robots"content="noindex"/>
為保證robots.txt的語法正確可使用百度Robots.txt工具生成
其他代碼
解除網(wǎng)頁右鍵屏蔽
javascript:(function(){functionR(a){ona="on"+a;if(window.addEventListener)window.addEventListener(a,function(e){for(varn=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");})()
把以上代碼加入收藏,在要解除屏蔽的網(wǎng)頁上點擊收藏鏈接
禁止其他網(wǎng)站iframe本站頁面
<script>
if(top!=self){top.location.href=self.location.href;}
</script>
推薦閱讀: