繁体   English   中英

通配符子域.htaccess和Codeigniter

[英]Wildcard subdomain .htaccess and Codeigniter

我正在尝试创建适当的.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,但是在内部,它将被重定向,就好像它是右侧的URL。

子域也将是动态的(即, http://user.domain.com不是实际的子域,而是.htaccess重写)

同样,/ home是我的默认控制器,因此没有子域会在内部将其强制为/ home控制器,并且其后的任何路径(如上面的#2示例所示)都将是该控制器内的(全部捕获)功能。

明智的做法是,如果传递了子域,它将作为(catch-all)控制器及其任何(catch-all)函数一起传递(如上面的#4示例所示)

希望我在这里没问太多,但我似乎无法为此找到正确的.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的格式不正确,我认为这可能是在搞砸了?

好吧,我相信我已经解决了。 到目前为止,这就是我所拥有的。

首先.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显示为/ foo / bar / 123 / ,这很完美

但如果我这样做: http://me.domain/foo/bar/123

然后我的PATH_INFO显示为/user/me/index.php/foo/bar/123/bar/123/多数情况下可以,但是为什么最后要重复这些参数?

所以,总的来说,我认为这是可行的。 我唯一要做的就是为我添加到\\ controllers的任何控制器设置多个路由。 除非有办法解决?

这应该工作。 请测试,让我知道它是否有效:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM