通配符子域的.htaccess和codeigniter通配符、htaccess、codeigniter

2023-09-02 00:41:02 作者:鹰行鹰素

我想创建正确的.htaccess,让我来映射为这样的:

I am trying to create the proper .htaccess that would allow me to map as such:

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。

Here, someone would type in the above URLs, however internally, it would be redirecting as if it were the URL on the right.

另外,根据子域将是动态的(即, http://user.domain.com 是不一个实际的子域,但将是一个重写的.htaccess)

Also the subdomain would be dynamic (that is, http://user.domain.com isn't an actual subdomain but would be a .htaccess rewrite)

此外/ home是我默认的控制器所以没有子域名会在内部它下面它强制/家居控制器和任何路径(如上图中所示#2的例子)将是控制器内(包罗万象)函数。

Also /home is my default controller so no subdomain would internally force it to /home controller and any paths following it (as shown in #2 example above) would be the (catch-all) function within that controller.

像明智的,如果一个子域传递它会得到通过为连同任何(包罗万象)功能,为它(包罗万象)控制器(如图#4上面的例子)

Like wise if a subdomain is passed it would get passed as a (catch-all) controller along with any (catch-all) functions for it (as shown in #4 example above)

我希望我不要求太多这里,但我似乎无法找出正确的.htaccess或路由规则(在codeigniter)这一点。

Hopefully I'm not asking much here but I can't seem to figure out the proper .htaccess or routing rules (in Codeigniter) for this.

httpd.conf文件和主机都安装就好了。

httpd.conf and hosts are setup just fine.

编辑#1

下面是我的.htaccess的即将关闭,但被搞乱了在某一点:

Here's my .htaccess that is coming close but is messing up at some point:

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(我已经删除了一些字段):

With the above, when I visit: http://test.domain/abc/123 this is what I notice in $_SERVER var (I've removed some of the fields):

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.phptesttestabc123abc123
    [PHP_SELF] => /index.php/test/abc/123
)

您可以看到PATH_TRANSLATED没有正确形成,我认为这可能会搞砸的事情了?

You can see the PATH_TRANSLATED is not properly being formed and I think that may be screwing things up?

推荐答案

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

Ok, I believe I have solved it. Here's what I have so far.

首先的.htaccess

First the .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文件

And finally 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";
?>

正如你可以从上面的一切工作看。我想不通的唯一的事情就是一个参数是重复的,当我使用子域名?

As you can see from above everything works. The only thing I can't figure out is why the last arguments are repeated when I use a subdomain?

如果我这样做:的http://域名/富/酒吧/ 123

然后我PATH_INFO显示为 /富/酒吧/ 123 / 这是完美

Then my PATH_INFO is shown as /foo/bar/123/ which is perfect

但是,如果我这样做: HTTP://me.domain/foo/bar/123

But if I do: http://me.domain/foo/bar/123

然后我PATH_INFO显示为 /user/me/index.php/foo/bar/123/bar/123 / 其中大部分是确定的,但为什么是重复到底参数?

Then my PATH_INFO is shown as /user/me/index.php/foo/bar/123/bar/123/ Which for the most part is OK but why is the parameters repeating in the end?

所以啊总的来说,我认为它的工作。唯一的事情我必须做的是有几个路线我添加到我的控制器任何控制器。除非有一个解决的方法?

So yea overall I think it's working. Only thing I'll have to do is have several routes for any controllers I add to my controllers. Unless there's a way around it?