PHP 404頁面/如何設置404頁面/URL靜態化/URL偽靜態化


php中如何設置404頁面及其他錯誤頁面

首先在項目根目錄下新建文件,文件名為" .htaccess "

在該文件中寫入一下配置項:

ErrorDocument 404 /404.html 或者是帶有文件路徑的地址: ErrorDocument 404 /error_pages/404.html

其他 401、500 等錯誤同理

注意點是:( 參考 )

  不要將錯誤頁面指向主頁面,可能會導致主頁在搜索引擎中消失;

  錯誤頁面地址設置用相對路徑(如果有絕對地址,會返回發起兩次請求,第一次是302重定向,重定向到你設置的絕對地址頁面,該頁面返回200)(自測)

.htaccess頁面其他設置( 參考 )

 

設置網頁301重定向

 

#從 old_dir 目錄重定向到 new_dir 目錄
Redirect /old_dir/ http://www.yourdomain.com/new_dir/index.html
#把通過二級目錄訪問的請求301重定向到二級域名
RedirectMatch 301 /dir/(.*) http://dir.yourdomain.com/$1

禁止指定IP段用戶的訪問

#禁止 IP 為 255.0.0.0 和 123.45.6.區段的 IP 訪問
order allow,deny
deny from 255.0.0.0
deny from 123.45.6.
allow from all

禁止指定來源網頁訪問

#禁止從 otherdomain.com 和 anotherdomain.com 的來源訪問
RewriteEngine on
# Options +FollowSymlinks
RewriteCond %{HTTP_REFERER} otherdomain\.com [NC,OR]
RewriteCond %{HTTP_REFERER} anotherdomain\.com
RewriteRule .* – [F]

圖片防盜鏈設置

#從本站以外的域名訪問圖片,一律顯示 feed.jpg
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?yourdomain.com/.*$ [NC]
RewriteRule \.(gif|jpg|png)$ http://www.yourdomain.com/feed.jpg [R,L]

設置文件夾首頁

#防止顯示文件夾列表,當訪問文件夾時,服務器會查找index.html,並將其做為首頁文件,如不存在依次向后查找
DirectoryIndex index.html index.cgi index.php

設置多媒體文件為可下載而非播放

AddType application/octet-stream .mp3 .mp4

自定義HTTP報頭

Header set X-Pingback “http://www.yourdomain.com/xmlrpc.php”
Header set article-by “yourdomain.com”

設置文件過期時間 Cache Control

# 啟用有效期控制
ExpiresActive On
# gif/png/jpg 有效期為1個月
ExpiresByType image/gif “access plus 1 month”
ExpiresByType image/png “access plus 1 month”
ExpiresByType image/jpg “access plus 1 month”
# js/css 有效期為1星期
ExpiresByType text/javascript “access plus 1 week”
ExpiresByType text/css “access plus 1 week”

WordPress建站程序偽靜態代碼

# BEGIN WordPress #這是一行注釋,表示 WordPress 的 htaccess 從這里開始
#如果Apache加載了mod_rewrite.c模塊,則運行以下代碼
RewriteEngine On #啟用 mod_rewrite 引擎
RewriteBase / #設置目錄重寫的基准URL為 /
RewriteRule ^index\.php$ – [L] #如果請求路徑是 index.php,停止重寫操作(避免死循環)
RewriteCond %{REQUEST_FILENAME} !-f #如果請求的不是一個文件,繼續處理
RewriteCond %{REQUEST_FILENAME} !-d #如果請求的不是一個目錄,繼續處理
RewriteRule . /index.php [L] #把所有的請求指向 /index.php
#結束 IfModule
# END WordPress #WordPress 的 htaccess 到這里結束

Discuz x3/x3.1通用偽靜態代碼

#如果Apache加載了mod_rewrite.c模塊,則運行以下代碼
RewriteEngine On
RewriteBase /discuz
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^topic-(.+)\.html$ portal.php?mod=topic&topic=$1&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^article-([0-9]+)-([0-9]+)\.html$ portal.php?mod=view&aid=$1&page=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^forum-(\w+)-([0-9]+)\.html$ forum.php?mod=forumdisplay&fid=$1&page=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ forum.php?mod=viewthread&tid=$1&extra=page\%3D$3&page=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^group-([0-9]+)-([0-9]+)\.html$ forum.php?mod=group&fid=$1&page=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^space-(username|uid)-(.+)\.html$ home.php?mod=space&$1=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^blog-([0-9]+)-([0-9]+)\.html$ home.php?mod=space&uid=$1&do=blog&id=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^archiver/(fid|tid)-([0-9]+)\.html$ archiver/index.php?action=$1&value=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^([a-z]+[a-z0-9_]*)-([a-z0-9_\-]+)\.html$ plugin.php?id=$1:$2&%1
#結束 IfModule

 

URL靜態化/URL偽靜態化

參考http://blog.csdn.net/shuiaaa/article/details/6561793

也可以參考我的另外一篇文章:PHP 頁面靜態化/純靜態化/偽靜態化

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM