第一步,從這里下載Apache。
第二步,解壓縮下載的Apache文件。這里我們下載的文件名假定為httpd-2.2.6.tar.gz。
tar xvfz httpd-2.2.6.tar.gz
第三步,編譯和安裝。這里我們將mod_rewrite模塊通過(guò)DSO機制在運行時(shí)動(dòng)態(tài)加載, apache被安裝在$PREFIX(比如/usr/local)下面。
cd httpd-2.2.6./configure --prefix=$PREFIX --enable-rewrite=sharedmakemake install
$PREFIX/bin/apachectl -k start
訪(fǎng)問(wèn)一下,看一下localhost可以看到apache的默認頁(yè)面。
此模塊提供了一個(gè)基于正則表達式分析器的重寫(xiě)引擎來(lái)實(shí)時(shí)重寫(xiě)URL請求。它支持每個(gè)完整規則可以擁有不限數量的子規則以及附加條件規則的靈活而且強大的URL操作機制。此URL操作可以依賴(lài)于各種測試,比如服務(wù)器變量、環(huán)境變量、HTTP頭、時(shí)間標記,甚至各種格式的用于匹配URL組成部分的查找數據庫。此模塊可以操作URL的所有部分(包括路徑信息部分),在服務(wù)器級的(httpd.conf)和目錄級的(.htaccess)配置都有效,還可以生成最終請求字符串。此重寫(xiě)操作的結果可以是內部子處理,也可以是外部請求的轉向,甚至還可以是內部代理處理。
比如,我們要將/~tom重定向到/u/tom,可以利用標準URL重定向。
RewriteRule ^/~([^/]+)/?(.*) /u/$1/$2 [R]
比如,將強制用戶(hù)使用www.example.com代替exaple.com。
RewriteCond %{HTTP_HOST} !^fully\.qualified\.domain\.name [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://fully.qualified.domain.name/$1 [L,R]
比如將指向/重定向到/about。
RewriteEngine onRewriteRule ^/$ /about/ [R]
比如,將/~user/重定向到http://newserver/~user/。
RewriteEngine onRewriteRule ^/~(.+) http://newserver/~$1 [R,L]
比如我們要實(shí)現這樣的一個(gè)域名www.username.host.domain.com(這點(diǎn)對于zeuux自由網(wǎng)站系統有用)。
RewriteEngine onRewriteCond %{HTTP_HOST} ^www\.[^.]+\.host\.com$RewriteRule ^(.+) %{HTTP_HOST}$1 [C]RewriteRule ^www\.([^.]+)\.host\.com(.*) /home/$1$2比如我們要實(shí)現一個(gè)這樣的功能,對于晚上和白天提供不同風(fēng)格的網(wǎng)頁(yè),可以這樣做。
RewriteEngine onRewriteCond %{TIME_HOUR}%{TIME_MIN} >0700RewriteCond %{TIME_HOUR}%{TIME_MIN} <1900RewriteRule ^index\.html$ index.day.htmlRewriteRule ^index\.html$ index.night.htm很多網(wǎng)絡(luò )爬蟲(chóng)不遵守在/robots.txt文件中定義的"Robot Exclusion Protocol", 可以禁掉。不如我們要保護/private/memeber
RewriteCond %{HTTP_USER_AGENT} ^NameOfBadRobot.*RewriteCond %{REMOTE_ADDR} ^123\.45\.67\.[8-9]$RewriteRule ^/private/memeber/.+ - [F]如果mod_rewrite不滿(mǎn)足你的要求,可以通過(guò)外部程序擴展。例如,
RewriteEngine onRewriteMap zeuux-map prg:/home/zeuux/map.zeuux.plRewriteRule ^/~zeuux/(.*)$ /~zeuux/${zeuux-map:$1}#!/path/to/perl# disable buffered I/O which would lead# to deadloops for the Apache server$| = 1;# read URLs one per line from stdin and# generate substitution URL on stdoutwhile (<>>) {s|^foo/|bar/|;print $_;}修改httpd.conf文件,確保有如下項:
# AccessFileName: The name of the file to look for in each directory
# for access control information.
#
AccessFileName .htaccess
# "/var/www/cgi-bin" should be changed to whatever your ScriptAliased
# CGI directory exists, if you have that configured.
#
AllowOverride AuthConfig
Options None
Order allow,deny
Allow from all
第一項說(shuō)明,用于身份認證的文件名是.htaccess,第二項是對你要限制的目錄進(jìn)行相應的配置,例子里是要限制cgi-bin目錄的訪(fǎng)問(wèn)。修改httpd.conf文件以后,需要重新啟動(dòng)httpd守護進(jìn)程,才能使配置生效。命令如下:
apachectl restart
在/etc目錄下,使用htpasswd命令建立password文件:
htpasswd -c htpasswd root
然后在待限制的目錄里(cgi-bin)建立.htaccess文件,內容如下:
AuthUserFile /etc/htpasswdAuthName "zeuux.org Auth"AuthType BasicRequire valid-user
此時(shí),已經(jīng)建立了對cgi-bin目錄的WEB訪(fǎng)問(wèn),即此時(shí)再通過(guò)WEB訪(fǎng)問(wèn)該目錄內的內容時(shí),需要如下的認證:
用戶(hù):root
口令:xxx
―――――――――――――――――――――――――――――――――――――――
設定apache的URL訪(fǎng)問(wèn)控制功能:
在httpd.conf里做如下的設定:
< Location /cgi-bin/cvsweb.cgi/ns210402/src/kernel>Order deny,allowDeny from allAllow from 10.1.2.30 10.1.2.91 10.1.2.230 10.1.2.15</Location>
經(jīng)過(guò)上面的設定,/cgi-bin/cvsweb.cgi/ns210402/src/kernel目錄(WEB宿主目錄)只能由上述幾個(gè)IP地址訪(fǎng)問(wèn)。實(shí)現了基于URL的安全功能。
聯(lián)系客服