1. 程式人生 > 其它 >【分享】apache 網頁301重定向、自定義400/403/404/500錯誤頁面

【分享】apache 網頁301重定向、自定義400/403/404/500錯誤頁面

首先簡單介紹一下,.htaccess檔案是Apache伺服器中的一個配置檔案(Nginx伺服器沒有),它負責相關目錄下的網頁配置。通過對.htaccess檔案進行設定,可以幫我們實現:網頁301重定向、自定義400/403/404/500錯誤頁面、改變副檔名、允許/阻止指定IP使用者訪問、禁止目錄列表、配置預設文件等功能,可以說是功能非常強大,下面就給大家介紹一下最常用的幾個功能的設定方法。

設定網站錯誤頁面

ErrorDocument 400 /error_pages/400.html
ErrorDocument 401 /error_pages/401.html
ErrorDocument 403 /error_pages/403.html
ErrorDocument 404 /error_pages/404.html
ErrorDocument 500 /error_pages/500.html

設定網頁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

本文轉自拉風的牛;和https://cycy.sourceforge.io/2021/12/apachdingxiang/