思(si)途CMS系統在Apache服務器(qi)上如何設置偽靜態
環境(jing):
系(xi)統(tong) Windows
Apache 2.2
加(jia)載Rewrite模(mo)塊(kuai):
在conf目錄下(xia)httpd.conf中(zhong)找到(dao)
LoadModule rewrite_module modules/mod_rewrite.so
這句,去掉(diao)前邊的注釋(shi)符號“#”,或添(tian)加這句。
允許在任何目錄(lu)中使用“.htaccess”文件,將“AllowOverride”改(gai)成“All”(默(mo)認為“None”):
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be “All”, “None”, or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride All
在Windows系統下不能直(zhi)接建立“.htaccess”文件,可以在命令行(xing)下使(shi)用“echo a> .htaccess”建(jian)立,然后使用記事(shi)本編輯。
Apache Rewrite模塊的(de)簡單應用:
Rewrite的所有判斷規(gui)則均基于Perl風格(ge)的正則表達式,通過(guo)以下基礎示例能寫出符合自己(ji)跳轉需求的代(dai)碼。
1、請求跳轉
目(mu)的(de)是如果請求(qiu)為.jsp文(wen)件(jian),則跳轉至其它域名訪問。
例如:訪問www.clin003.com/a.php跳(tiao)轉至b.clin003.com/b.php網頁,訪問www.clin003.com/news/index.php跳轉至b.clin003.com/news/index.php網頁
注(zhu)意:不是(shi)使用HTML技術中的meta或(huo)者javascript方式,因為www.clin003.com/a.php這個文(wen)件并不(bu)存在,用的是Apache2.2服務器中的(de)Rewrite模塊。
修改 .htaccess或apche的配置文件httpd.conf文件,添加(jia)以(yi)下內(nei)容
RewriteEngine on
#開啟Rewrite模塊
RewriteRule (.*).php$ http://b.clin003.com/$1.jsp [R=301,L,NC]
#截獲(huo)所有.jsp請求(qiu),跳轉到//b.clin003.com/加上原來的請求再加上.php。R=301為301跳轉(zhuan),L為rewrite規則到此終止,NC為不區分大小寫
2、域名跳轉(zhuan)
如果請求為old.clin003.com下的所有URL,跳轉至(zhi)b.clin003.com
RewriteEngine on
#開啟Rewrite模塊
RewriteCond %{REMOTE_HOST} ^old.studenthome.cn$ [NC]
#針對host為old.clin003.com的主機(ji)做(zuo)處理(li),^為開始(shi)字符,$為結尾字符(fu)
RewriteRule (.*) http://b.clin003.com/$1 [R=301,L,NC]
3、防盜(dao)鏈
如果本網站的圖片不想讓其它網站調用(yong),可以(yi)在(zai) .htaccess或(huo)者apche的配置文件httpd.conf文件中添加以下內容
代碼
RewriteEngine on
#開啟Rewrite模塊
RewriteCond %{HTTP_REFERER} !^$
#如(ru)果不(bu)是直接輸入圖(tu)片地址(zhi)
RewriteCond %{HTTP_REFERER} !img.clin003.com$ [NC]
#且如(ru)果不是img.clin003.com所(suo)有子域名(ming)調用的
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]
#截獲(huo)所有.jpg或.jpeg……請求,跳轉到(dao)//clin003.com/err.jpg提(ti)示錯誤的圖片(pian),注:該圖片(pian)不能在原域名下,也(ye)不能在該.htaccess文件(jian)有效控制的文件(jian)夾中
4、不需要定義.htaccess文(wen)件
在Apache2confhttpd.conf 最(zui)后(hou)一行添加
RewriteEngine On
RewriteRule ^(.*)-htm-(.*)$ $1.php?$2
重啟Apache
登陸(lu)后臺(tai)開啟(qi)全偽
GD的Linux主(zhu)機安(an)裝discuz 7.2的注意了
這個discuz官方給(gei)出的偽靜態規則
代碼
# 將 RewriteEngine 模式(shi)打開
RewriteEngine On
# 修改(gai)以下語句中的 /discuz 為你的論壇目錄地址,如果程序放在根目錄中,請將 /discuz 修(xiu)改為 /
RewriteBase /discuz
# Rewrite 系統規則(ze)請(qing)勿修改
RewriteRule ^archiver/((fid|tid)-[w-]+.html)$ archiver/index.php?$1
RewriteRule ^forum-([0-9]+)-([0-9]+).html$ forumdisplay.php?fid=$1&page=$2
RewriteRule ^thread-([0-9]+)-([0-9]+)-([0-9]+).html$ viewthread.php?tid=$1&extra=page%3D$3&page=$2
RewriteRule ^space-(username|uid)-(.+).html$ space.php?$1=$2
RewriteRule ^tag-(.+).html$ tag.php?name=$1
使用這(zhe)個規則后,你會發現,點(dian)擊論壇右下角的網站地圖(tu)“Archiver”,只能(neng)看到板塊,不(bu)能(neng)打(da)開(kai)板塊下(xia)的帖(tie)子(zi)
這是(shi)修改后的(de)偽(wei)靜(jing)態規則:
代碼(ma)
# 將 RewriteEngine 模式打開(kai)
RewriteEngine On
# 修改以下(xia)語句中的 /discuz 為你(ni)的論壇目錄(lu)地址,如果程序放在(zai)根目錄(lu)中,請將 /discuz 修改(gai)為 /
RewriteBase /
# Rewrite 系統規則請勿修改
RewriteRule ^archiver/([a-z0-9-]+.html)$ archiver/index.php?$1
RewriteRule ^forum-([0-9]+)-([0-9]+).html$ forumdisplay.php?fid=$1&page=$2
RewriteRule ^thread-([0-9]+)-([0-9]+)-([0-9]+).html$ viewthread.php?tid=$1&extra=page%3D$3&page=$2
RewriteRule ^space-(username|uid)-(.+).html$ space.php?$1=$2
RewriteRule ^tag-(.+).html$ tag.php?name=$1
會員評論