网址重写=>变量到子域

时间:2013-05-19 14:19:15

标签: url-rewriting subdomain

在我的.htaccess文件中,此代码可以正常工作:

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

例如,当我写这个URL时:

http://XXX.mydomain.com

重定向到:

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

但问题在于我只是想要相反:

当我写URL时:

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

我想重定向到:

http://XXX.mydomain.com

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

有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

试试这个

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
相关问题