【问题标题】:Redirecting dynamic(variable) subdomain to a controller using .htaccess使用 .htaccess 将动态(变量)子域重定向到控制器
【发布时间】:2013-12-20 14:30:02
【问题描述】:

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

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

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

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

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

【问题讨论】:

  • RewriteCond 捕获到 %1 而不是 $1。这可能就是您在这里所需要的——其他一切看起来都正确。
  • 但是你需要像http://user1.example.com/path那样将URI附加到它上面吗--> http://example.com/index.php/users/index/user1/path
  • @MichaelBerkowski 现在我不需要附加任何东西,我只想将子域转换为不带参数的用户页面
  • 另外,apache 或 mod_rewrite 是否保留了一个日志,我可以在其中检查它重定向到的位置,也许我可以从中了解哪里出了问题。
  • 您必须手动启用RewriteLoghttpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritelog 并设置一个 RewriteLogLevel 来确定您获得了多少细节。

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


【解决方案1】:

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

试试这个规则:

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

【讨论】:

  • 你的规则还是一样的结果(这个网页不可用),你能建议什么吗?谢谢。
  • 规则正确,但您的设置可能与预期不同。 user1.mydomain.com 的 DocumentRoot 是否与 mydomain.com 相同?
  • 上面.htaccess的位置是什么?当您使用上述规则输入http://user1.mydomain.com/ 时会发生什么?
  • 我认为我没有为子域设置文档根目录。鉴于 user1 是可变的,我该怎么做?
  • 我没有很多服务器管理知识,但我认为user1.mydomain.comuser2.mydomain.com 等需要分开VirtualHost 条目。
猜你喜欢
  • 2020-08-21
  • 2012-05-19
  • 1970-01-01
  • 2014-08-19
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多