[Apache] mod_rewrite 進階用法,網域 redirect

我對 mod_rewrite 的技術相當有興趣,最近又有人在 ptt Linux 版上問到這相關的問題,如下

我用的是虛擬主機 (web hosting) 他的管理是這樣的, 不論代管哪個domain 一律都是對應到 /home/user/public_html 這個目錄下 也就是說, 1.com 跟 2.com 都會對到 ~/public_html 問題是: –> 我希望有人輸入 “1.com” 時, 就是讀 ~/public_html/data1 –> 有人讀 “2.com” 時, 就是讀 ~/public_html/data2 當然目前如果都不做設定, 那就是要用 http;//1.com/data1/ 跟 http;//2.com/data2/ 才能答成相同功能. 請問 .htaccess 有辦法改寫這樣的狀況嗎.

我的解法如下 RewriteCond %{HTTP_HOST} !^1.com [NC] RewriteRule ^data1/(.*) http://1.com/data1/$1 [R,L] RewriteCond %{HTTP_HOST} !^2.com [NC] RewriteRule ^data2/(.*) http://2.com/data2/$1 [R,L] 當然,板上 sayya bbs 的 myturn 兄提供另一種解法 #你可試試看使用 redirect 來做,文件可參考下列 http://httpd.apache.org/docs/2.0/mod/mod_alias.html#redirect redirect 302 /home/user/public_html/data1/ http://1.com/data1/ redirect 302 /home/user/public_html/data2/ http://2.com/data2/ #也可參考下列的自動建立工具 #http://www.linuxkungfu.org/tools/htaccesser/ 由此可見 mod_rewrite 的強大功能,當然他不只有這樣的功能,還有其他功能如下 Redirect /yahoo http://tw.yahoo.com 指定網址 /yahoo 會轉向到 http://tw.yahoo.com RewriteCond %{HTTP_HOST} ^wu-boy.com [NC] RewriteRule (.*) http://www.wu-boy.com/$1 [R,L] 這個是用來改變網域,如果你有一個網域是 wu-boy.com,可是你要改成 www.wu-boy.com,就可以利用上面來達成,底下是我轉錄的一些說明,大家可以看看

RewriteCond

The RewriteCond directive defines a rule condition. Preserve a RewriteRule with one or more RewriteCond directives. The following rewriting rule is only used if its pattern matches the current state of the URI and if these additional conditions apply too. You can set special flags for condition pattern by appending a third argument to the RewriteCond directive. Flags is a comma-separated list of the following flags: [NC] (No Case) This makes the condition pattern case insensitive, no difference between ‘A-Z’ and ‘a-z’. [OR] (OR next condition) Used to combinate rule conditions with a OR. RewriteRule

The RewriteRule directive is the real rewriting. You can set special flags for condition pattern by appending a third argument to the RewriteCond directive. Flags is a comma-separated list of the following flags: [R] (force Redirect) Redirect the URL to a external redirection. Send the HTTP response, 302 (MOVED TEMPORARILY). [F] (force URL to be Forbidden) Forces the current URL to be forbidden. Send the HTTP response, 403 (FORBIDDEN). [G] (force URL to be Gone) Forces the current URL to be gone. Send the HTTP response, 410 (GONE). [L] (last rule) Forces the rewriting processing to stop here and don’t apply any more rewriting rules. [P] (force proxy) This flag forces the current URL as a proxy request and put through the proxy module mod_proxy.Condition pattern

There are some special variants of CondPatterns. Instead of real regular expression strings you can also use one of the following: < Condition (is lower than Condition) Treats the Condition as a string and compares it to String. True if String is lower than Condition. > Condition (is greater than Condition) Treats the Condition as a string and compares it to String. True if String is greater than CondPattern. = Condition (is equal to Condition) Treats the Condition as a string and compares it to String. True if String is equal to CondPattern. -d (is directory) Treats the String as a pathname and tests if it exists and is a directory. -f (is regular file) Treats the String as a pathname and tests if it exists and is a regular file. -s (is regular file with size) Treats the String as a pathname and tests if it exists and is a regular file with size greater than zero. -l (is symbolic link) Treats the String as a pathname and tests if it exists and is a symbolic link. -F (is existing file via sub request) Checks if String is a valid file and accessible via all the server’s currently configured access controls for that path. Use it with care because it decreases your servers performance! -U (is existing URL via sub request) Checks if String is a valid URL and accessible via all the server’s currently configured access controls for that path. Use it with care because it decreases your servers performance! NOTE: You can prefix the pattern string with a ‘!’ character (exclamation mark) to specify a non-matching pattern. 再來底下有一些例子,大家可以看看,我看過了,覺得還不錯

Protecting your images and files from linking DESCRIPTION: In some cases other webmasters are linking to your download files or using images, hosted on your server as inline-images on their pages. RewriteEngine On RewriteCond %{HTTP_REFERER} !^$ [NC] RewriteCond %{HTTP_REFERER} !^http://domain.com [NC] RewriteCond %{HTTP_REFERER} !^http://www.domain.com [NC] RewriteCond %{HTTP_REFERER} !^http://212.204.218.80 [NC] RewriteRule ^.*$ http://www.domain.com/ [R,L] EXPLAIN: In this case are the visitors redirect to http://www.domain.com/ if the hyperlink has not arrived from http://domain.com, http://www.domain.com or http://212.204.218.80. top Redirect visitor by domain name DESCRIPTION: In some cases the same web site is accessible by different addresses, like domain.com, www.domain.com, www.domain2.com and we want to redirect it to one address. RewriteEngine On RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC] RewriteRule ^(.*)$ http://www.domain.com/$1 [R,L] EXPLAIN: In this case the requested URL http://domain.com/foo.html would redirected to the URL http://www.domain.com/foo.html. top Redirect domains to other directory RewriteEngine On RewriteCond %{HTTP_HOST} ^www.domain.com$ RewriteCond %{REQUEST_URI} !^/HTML2/ RewriteRule ^(.*)$ /HTML2/$1 top Redirect visitor by user agent DESCRIPTION: For important top level pages it is sometimes necesarry to provide pages dependend on the browser. One has to provide a version for the latest Netscape, a version for the latest Internet Explorer, a version for the Lynx or old browsers and a average feature version for all others. # MS Internet Explorer – Mozilla v4 RewriteEngine On RewriteCond %{HTTP_USER_AGENT} ^Mozilla/4(.*)MSIE RewriteRule ^index.html$ /index.IE.html [L] # Netscape v6.+ – Mozilla v5 RewriteCond %{HTTP_USER_AGENT} ^Mozilla/5(.)Gecko RewriteRule ^index.html$ /index.NS5.html [L] # Lynx or Mozilla v1/2 RewriteCond %{HTTP_USER_AGENT} ^Lynx/ [OR] RewriteCond %{HTTP_USER_AGENT} ^Mozilla/[12] RewriteRule ^index.html$ /index.20.html [L] # All other browsers RewriteRule ^index.html$ /index.32.html [L] EXPLAIN: In this case we have to act on the HTTP header User-Agent. If the User-Agent begins with Mozilla/4 and is MS Internet Explorer (MSIE), the page index.html is rewritten to index.IE.html and the rewriting stops. If the User-Agent begins with Mozilla/5 and is Netscape (Gecko), the page index.html is rewritten to index.NS5.html. If the User-Agent begins with Lynx/ or Mozilla/1,2, the page index.html is rewritten to index.20.html. All other browsers receive page index.32.html 也可以寫到 virtualHost 裡面 <VirtualHost *> ServerAdmin xxx@xxx DocumentRoot “/var/ww/html/Discuz” ServerName xxx.xxx.cc RewriteEngine On RewriteRule ^(.*)/archiver/([a-z0-9-]+.html)$ $1/archiver/index.php?$2 RewriteRule ^(.*)/forum-([0-9]+)-([0-9]+).html$ $1/forumdisplay.php?fid=$2&page=$3 RewriteRule ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+).html$ $1/viewthread.php?tid=$2&extra=page%3D$4&page=$3 RewriteRule ^(.*)/profile-(username|uid)-(.+).html$ $1/viewpro.php?$2=$3 RewriteRule ^(.*)/space-(username|uid)-(.+).html$ $1/space.php?$2=$3 RewriteRule ^(.)/favicon.ico$ /favicon.ico reference http://www.widexl.com/scripts/documentation/mod_rewrite.html http://httpd.apache.org/docs/2.0/mod/mod_alias.html#redirect http://httpd.apache.org/docs/2.0/misc/rewriteguide.html http://bbs.ecstart.com/viewthread.php?action=printable&tid=5473


See also