使用.htaccess将动态(变量)子域重定向到控制器

时间:2013-12-20 14:30:03

标签: apache .htaccess mod-rewrite url-rewriting

我正在尝试创建user1.mydomain.com格式的用户页面网址,我需要将其重定向到http://mydomain.com/index.php/users/index/user1

在我的.htaccess文件中,我有以下内容:

RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.com$
RewriteRule (.*) http://mydomain.com/index.php/users/index/$1 [L]

我从谷歌浏览器获取“此网页不可用”,至少不是服务器错误。

我做错了什么?此外,在rewriterule模式中,它如何知道$ 1引用子域而不是完整的url?是因为我的重写中有%{HTTP_HOST}吗?

1 个答案:

答案 0 :(得分:2)

来自RewriteCond的捕获变量表示为%1%2等...

尝试此规则:

RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com$ [NC]
RewriteRule ^ http://mydomain.com/index.php/users/index/%1 [L,R]