2011-01-08 53 views
2

我試圖創建正確的.htaccess,讓我映射爲這樣:通配符子域的.htaccess和笨

http://domain.com/    --> http://domain.com/home 
http://domain.com/whatever  --> http://domain.com/home/whatever 
http://user.domain.com/   --> http://domain.com/user 
http://user.domain.com/whatever --> http://domain.com/user/whatever/ 

這裏,有人會鍵入上述網址,但在內部,這將是重定向就好像它是右邊的URL一樣。

而且子域是動態的(也就是說,http://user.domain.com不是實際的子域,但將是一個重寫的.htaccess)

而且/ home是我的默認控制器所以沒有子域名會在內部迫使它到/ home控制器及其之後的任何路徑(如上面的#2示例所示)將是該控制器內的(全部捕捉)功能。

像明智的,如果一個子域被傳遞它會得到與任何(全捕獲)功能它沿着一個(全捕獲)控制器傳遞(如上面的#4的例子)

希望I」 m在這裏不會問很多,但我似乎無法弄清楚適當的.htaccess或路由規則(在Codeigniter中)。

httpd.conf和主機設置得很好。

編輯#1

這裏是我的.htaccess是即將接近,但在某些時候是搞亂:

RewriteEngine On 

RewriteBase/
RewriteCond %{SCRIPT_FILENAME} !-d 
RewriteCond %{SCRIPT_FILENAME} !-f 

RewriteCond %{HTTP_HOST} ^([a-z0-9-]+).domain [NC] 
RewriteRule (.*) index.php/%1/$1 [QSA] 

RewriteCond $1 !^(index\.php|images|robots\.txt) 
RewriteRule ^(.*)$ /index.php/$1 [L,QSA] 

通過上述,當我訪問:http://test.domain/abc/123這是我通知在$ _SERVER var(我已經刪除了一些字段):

Array 
(
    [REDIRECT_STATUS] => 200 
    [SERVER_NAME] => test.domain 
    [REDIRECT_URL] => /abc/123 
    [QUERY_STRING] => 
    [REQUEST_URI] => /abc/123 
    [SCRIPT_NAME] => /index.php 
    [PATH_INFO] => /test/abc/123 
    [PATH_TRANSLATED] => redirect:\index.php\test\test\abc\123\abc\123 
    [PHP_SELF] => /index.php/test/abc/123 
) 

您可以看到PATH_TRANSLATED不正確醫學和我認爲這可能會搞砸了嗎?

回答

-1

這應該工作。請測試,讓我知道它是否工作:

RewriteEngine On 
RewriteCond %{HTTP_HOST}    ^[^.]+\.domain\.com$ 
RewriteRule ^(.+)      %{HTTP_HOST}$1  [C] 
RewriteRule ^([^.]+)\.domain\.com(.*) /$1$2     [L] 
RewriteRule ^(.*)      /home$1    [L] 
+1

謝謝羚牛,不幸的是沒有工作。它導致服務器500(由於可能的配置錯誤,請求超過了10個內部重定向的限制) – Gautam 2011-01-08 21:17:25

0

好吧,我相信我已經解決了它。這是迄今爲止我所擁有的。

首先的.htaccess

RewriteEngine On 

RewriteBase/

# if REQUEST_URI contains the word "user" and the 
# SERVER_NAME doesn't contain a "." re-direct to the root 
# The reason this is done is because of how the last two rules 
# below are triggered 
RewriteCond %{REQUEST_URI} (user) [NC] 
RewriteCond %{SERVER_NAME} !\. 
RewriteRule (.*)/[L,R=301] 

# Allow files and directories to pass 
RewriteCond %{SCRIPT_FILENAME} !-d 
RewriteCond %{SCRIPT_FILENAME} !-f 

# Codeigniter rule for stripping index.php 
RewriteCond $1 !^(index\.php|images|robots\.txt) 
RewriteRule ^(.*)$ /index.php/$1 [C] 

# Force wild-card subdomains to redirect. 
# E.g. http://me.domain/foo/bar/123 as http://domain/user/me/index.php/foo/bar/123/bar/123/ 
RewriteCond %{HTTP_HOST} ^([a-z0-9-]+).domain [NC] 
RewriteRule (.*) /index.php/user/%1/$1/ [L] 

最後routes.php文件

<?php 
// Force routing to userhome controller if URL contains the word "user" 
// otherwise force everything else to home controller 
$route['user/:any'] = "userhome"; 
$route[':any'] = "home"; 
?> 

正如你可以從上面的一切工作看。我唯一無法弄清楚的是爲什麼當我使用子域時最後一個參數會重複出現?

如果我做的:http://domain/foo/bar/123

然後我PATH_INFO顯示爲/富/酒吧/ 123/這是完美

但是,如果我做的:http://me.domain/foo/bar/123

然後顯示我的PATH_INFO作爲/user/me/index.php/foo/bar/123/bar/123/ 大多數情況下是好的,但爲什麼參數重複呢?

因此,總體來說,我認爲它的工作。唯一需要做的是爲我的\ controllers添加的任何控制器提供幾條路由。除非有解決方法嗎?