Apache開啟偽靜態示例
Apache開啟偽靜態示例
環境:
系統 Windows
Apache 2.2
載入Rewrite模組:
在conf目錄下httpd.conf中找到
LoadModule rewrite_module modules/mod_rewrite.so
這句,去掉前邊的註釋符號“#”,或新增這句。
允許在任何目錄中使用“.htaccess”檔案,將“AllowOverride”改成“All”(預設為“None”):
# AllowOverride controls what directives may be placed in .htaccess files.# It can be “All”, “None”, or any combination of the keywords:
在Windows系統下不能直接建立“.htaccess”檔案,可以在命令列下使用“echo a> .htaccess”建立,然後使用記事本編輯。
Apache Rewrite模組的簡單應用:
Rewrite的所有判斷規則均基於Perl風格的正則表示式,通過以下基礎示例能寫出符合自己跳轉需求的程式碼。
1、請求跳轉
目的是如果請求為.jsp檔案,則跳轉至其它域名訪問。
例如:訪問www.clin003.com/a.php跳轉至b.clin003.com/b.php網頁,訪問www.clin003.com/news/index.php
注意:不是使用HTML技術中的meta或者javascript方式,因為www.clin003.com/a.php這個檔案並不存在,用的是Apache2.2伺服器中的Rewrite模組。
修改 .htaccess或apche的配置檔案httpd.conf檔案,新增以下內容
RewriteEngine on#開啟Rewrite模組RewriteRule (.*)\.php$ http://b.clin003.com/$1\.jsp [R=301,L,NC]#截獲所有.jsp請求,跳轉到http://b.clin003.com/加上原來的請求再加上.php。R=301為301跳轉,L為rewrite規則到此終止,NC為不區分大小寫
2、域名跳轉
如果請求為old.clin003.com下的所有URL,跳轉至b.clin003.com
RewriteEngine on#開啟Rewrite模組RewriteCond %{REMOTE_HOST} ^old.studenthome.cn$ [NC]#針對host為old.clin003.com的主機做處理,^為開始字元,$為結尾字元RewriteRule (.*) http://b.clin003.com/$1 [R=301,L,NC]
3、防盜鏈
如果本網站的圖片不想讓其它網站呼叫,可以在 .htaccess或者apche的配置檔案httpd.conf檔案中新增以下內容
程式碼
RewriteEngine on#開啟Rewrite模組RewriteCond %{HTTP_REFERER} !^$#如果不是直接輸入圖片地址RewriteCond %{HTTP_REFERER} !img.clin003.com$ [NC]#且如果不是img.clin003.com所有子域名呼叫的RewriteCond %{HTTP_REFERER} !img.clin003.com/(.*)$ [NC]RewriteCond %{HTTP_REFERER} !zhuaxia.com [NC]RewriteCond %{HTTP_REFERER} !google.com [NC]RewriteCond %{HTTP_REFERER} !google.cn [NC]RewriteCond %{HTTP_REFERER} !baidu.com [NC]RewriteCond %{HTTP_REFERER} !feedsky.com [NC]RewriteRule (.*)\.(jpg|jpeg|jpe|gif|bmp|png|wma|mp3|wav|avi|mp4|flv|swf)$ http://clin003.com/err.jpg [R=301,L,NC]#截獲所有.jpg或.jpeg……請求,跳轉到http://clin003.com/err.jpg提示錯誤的圖片,注:該圖片不能在原域名下,也不能在該.htaccess檔案有效控制的資料夾中
4、不需要定義.htaccess檔案
在Apache2\conf\httpd.conf 最後一行新增
RewriteEngine OnRewriteRule ^(.*)-htm-(.*)$ $1.php?$2
重啟Apache
登陸後臺開啟全偽
GD的Linux主機安裝discuz 7.2的注意了
這個discuz官方給出的偽靜態規則
程式碼
# 將 RewriteEngine 模式開啟RewriteEngine On# 修改以下語句中的 /discuz 為你的論壇目錄地址,如果程式放在根目錄中,請將 /discuz 修改為 /RewriteBase /discuz# Rewrite 系統規則請勿修改RewriteRule ^archiver/((fid|tid)-[\w\-]+\.html)$ archiver/index.php?$1RewriteRule ^forum-([0-9]+)-([0-9]+)\.html$ forumdisplay.php?fid=$1&page=$2RewriteRule ^thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ viewthread.php?tid=$1&extra=page\%3D$3&page=$2RewriteRule ^space-(username|uid)-(.+)\.html$ space.php?$1=$2RewriteRule ^tag-(.+)\.html$ tag.php?name=$1
使用這個規則後,你會發現,點選論壇右下角的網站地圖“Archiver”,只能看到板塊,不能開啟板塊下的帖子
這是修改後的偽靜態規則:
程式碼
# 將 RewriteEngine 模式開啟RewriteEngine On# 修改以下語句中的 /discuz 為你的論壇目錄地址,如果程式放在根目錄中,請將 /discuz 修改為 /RewriteBase /# Rewrite 系統規則請勿修改RewriteRule ^archiver/([a-z0-9\-]+\.html)$ archiver/index.php?$1RewriteRule ^forum-([0-9]+)-([0-9]+)\.html$ forumdisplay.php?fid=$1&page=$2RewriteRule ^thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ viewthread.php?tid=$1&extra=page\%3D$3&page=$2RewriteRule ^space-(username|uid)-(.+)\.html$ space.php?$1=$2RewriteRule ^tag-(.+)\.html$ tag.php?name=$1