【问题标题】:URL Rewrite => Variable to SubdomainURL重写=>变量到子域
【发布时间】:2013-05-19 14:19:14
【问题描述】:

在我的 .htaccess 文件中,这段代码运行良好:

RewriteCond %{HTTP_HOST} ^(.+).mydomain.com$ 
RewriteRule ^(.*)$ http://mydomain.com/profile.php?username=%1 [R,L]

例如,当我写这个网址时:

http://XXX.mydomain.com

它重定向到:

http://mydomain.com/profile.php?username=XXX

但问题是我只是想要相反的:

当我写网址时:

http://www.mydomain.com/profile.php?username=XXX

我想要重定向到:

http://XXX.mydomain.com

我尝试了所有可能性,但没有任何效果。

有人可以帮帮我吗?

【问题讨论】:

  • 我的回答有帮助吗?你搞定了吗?

标签: url-rewriting subdomain


【解决方案1】:

试试这个

RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} !^(www\.)?mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.com$ [NC]
RewriteRule ^.*$ http://www.mydomain.com/profile.php?username=%1 [L]

好的,www 现在是可选的。

编辑:同样重定向sub.domain.com/someother/page.php

RewriteCond %{HTTP_HOST} !^(www\.)?mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.com$ [NC]
RewriteRule ^(profile\.php)?$ http://www.mydomain.com/profile.php?username=%1 [L]
RewriteRule ^(.+)$ http://www.mydomain.com/$1 [L]

这将重定向

eli.mydomain.com => http://www.mydomain.com/profile.php?username=eli
eli.mydomain.com/profile.php => http://www.mydomain.com/profile.php?username=eli

eli.mydomain.com/contactus.php => http://www.mydomain.com/contactus.php
eli.mydomain.com/help/search.php?q=faq => http://www.mydomain.com/help/search.php?q=faq

【讨论】:

  • 那又不行了。我想要所有 URL 的“www”,但不是那些:[xxx.mydomain.com] 您的代码将 url 重定向:[xxx.mydomain.com] 到 [mydomain.com/index.php]
  • @Eli 实际上恰恰相反!您的 .htaccess 中是否还有其他规则,因为我的规则不会针对 sub.mydomain.com 触发。您可以发布您的 .htaccess 吗?
  • 确定:SetEnv PHP_VER 5_3 SetEnv REGISTER_GLOBALS 0 选项 +FollowSymLinks RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} ^(.+).mydomain.com$ RewriteRule ^(.*)$ mydomain.com/profile.php?username=%1 [ R,L]
  • @Eli 我认为您发布的内容与我的回答不符。 RewriteCond 和 RewriteRule 都不符合我在答案中发布的内容。您必须更改规则才能看到它们有效!
  • 这是我原来的 .htaccess 但是通过测试你的我已经删除了我的 RewriteCond 和 RewriteRule
猜你喜欢
  • 2018-10-22
  • 1970-01-01
  • 2010-11-08
  • 1970-01-01
  • 1970-01-01
  • 2012-09-17
  • 1970-01-01
  • 2019-07-02
相关资源
最近更新 更多