我們把我們的 .htaccess文件放到網站的跟目錄下了;
.htaccess文件的內容如下:
RewriteCond %{HTTP_HOST} ask.xxoo.com
RewriteRule ^(index.html)?$ /ask/
RewriteRule ^(.*)list-([0-9]+)-([a-z]+)\.html$ $1/ask/list.php?catid=$2&action=$3
RewriteRule ^(.*)show-([0-9]+)\.html$ $1/ask/show.php?id=$2
RewriteCond %{HTTP_HOST} space.xxoo.com
RewriteRule ^(index.html)?$ /forum/forum.php (注:此處dx系統的二級域名設置錯誤,少了一步,空間,論壇的二級域名設置錯誤,上面的問答模塊二級域名設置沒有錯誤,請看教程四)
錯誤原因:原來我設置錯誤的原因是我想直接通過url重寫來做空間、論壇的二級域名設置,但是dx裝在根目錄的/forum/文件夾里。這樣也可以設置二級域名,但會報錯,比如,論壇雖然可以過于,但是發帖,等操作連接還是錯誤的,會自動吧安裝目錄添加到url中。處處出錯,其實一開始我走的道路都是錯誤的。
上面只做了問答模塊和空間模塊的url重寫。我們來分析一下代碼。
上面的代碼中有兩個 RewriteCond 段,意思是一個url地址會于RewriteCond 后面的正則匹配,如果成功則與對于RewriteCond 下的RewriteRule 進行在匹配,如果還能配的上url重定向的 RewriteRule 第二個參數里的url地址了,
url解析流程是這樣的:我們咋地址欄中輸入 ask.xxoo.com ,域名泛解析到我們規定的ip地址也就是網站上,網站啟用url重寫來過濾url地址,過濾ask.xxoo.com被第一個RewriteCond命中,然后再與這個RewriteCond下面所屬的3個RewriteRule匹配,被第一個RewriteRule命中,命中返回的是/ask/ 所以ask.xxoo.com被重定向到了ask.xxoo.com/ask/文件夾了
分析代碼:
RewriteCond 后面的 %{HTTP_HOST} 是域名
RewriteCond %{HTTP_HOST} space.xxoo.com 表示地址欄中的域名是否等于space.xxoo.com這里也可以使用正則 如:RewriteCond %{HTTP_HOST} space([1-9]).xxoo.com 這匹配的url包含了space1.xxoo.com space2.xxoo.com space2.xxoo.com .......space9.xxoo.com 這9個域名
RewriteRule ^(index.html)?$ /ask/ 匹配的是網站但域名ask.xxoo.com或ask.xxoo.com/index.html這兩個地址: 重定向的是ask.xxoo.com/ask
RewriteRule ^(.*)list-([0-9]+)-([a-z]+)\.html$ $1/ask/list.php?catid=$2&action=$3
匹配地址包含
ask.xxoo.com/list-3-all.html 重定向的是ask.xxoo.com/ask/list.php?catid=3&action=all
ask.xxoo.com/list-45-high.html 重定向的是ask.xxoo.com/ask/list.php?catid=45&action=hight
RewriteRule ^(.*)show-([0-9]+)\.html$ $1/ask/show.php?id=$2
匹配地址包含
ask.xxoo.com/show-3.html 重定向的是ask.xxoo.com/ask/show.php?id=3
ask.xxoo.com/show-21.html 重定向的是ask.xxoo.com/ask/show.php?id=21
。。。。。
總結:
RewriteRule 規則形式是:
RewriteRule 正則配表達式 url重定向地址 重定向地址中的參數都是通過正則用的引用來處理 :
RewriteCond 第一個參數中用到的 %{ NAME_OF_VARIABLE } ,服務器變量。 變量的名字如下表(分類顯示)