2016-04-27 59 views
1

我想用htaccess做一個多級子域重定向。即http://jon.doe.example.com/?number=120384重定向到http://www.example.com/lookup.php?firstname=joe&lastname=doe&number=120384多級子域htaccess重定向

這是我嘗試過,但它不工作。

RewriteEngine on 
RewriteCond %{HTTP_HOST} !^www.example.com 
RewriteCond %{HTTP_HOST} ^([^.]+).example.com 
RewriteRule ^/?$ /lookup.php?firstname=%1&lastname=%2&%{QUERY_STRING} [P,L,QSA] 

當我打電話http://jon.doe.example.com/?number=120384它只是重定向到www.example.com

回答

2

您應該修改代碼在.htaccess如下:

RewriteEngine on 
RewriteCond %{HTTP_HOST} !^www.example.com 
RewriteCond %{HTTP_HOST} ^(.*)\.(.*)\.example\.com$ 
RewriteRule ^/\?number=(.*)$ /lookup.php?firstname=%1&lastname=%2&number=$1 [P,L,QSA] 

這裏的第二級子域名會被提取在參數%1中,第一級子域%2。查詢字符串參數號的值將在$ 1中可用。

+0

謝謝,這工作得很好。 :) – Teeoney