2011-01-08 57 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添加的任何控制器提供几条路由。除非有解决方法吗?