簡體   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