Url rewriting URL重写=>;变量到子域

Url rewriting URL重写=>;变量到子域,url-rewriting,subdomain,Url Rewriting,Subdomain,在my.htaccess文件中,以下代码运行良好: RewriteCond %{HTTP_HOST} ^(.+).mydomain.com$ RewriteRule ^(.*)$ http://mydomain.com/profile.php?username=%1 [R,L] 重写cond%{HTTP_HOST}^(+).mydomain.com$ 重写规则^(.*)$http://mydomain.com/profile.php?username=%1 [R,L] 例如,当我编写此UR

在my.htaccess文件中,以下代码运行良好:

RewriteCond %{HTTP_HOST} ^(.+).mydomain.com$
RewriteRule ^(.*)$ http://mydomain.com/profile.php?username=%1 [R,L] 重写cond%{HTTP_HOST}^(+).mydomain.com$
重写规则^(.*)$http://mydomain.com/profile.php?username=%1 [R,L] 例如,当我编写此URL时:

http://XXX.mydomain.com http://XXX.mydomain.com 它重定向到:

http://mydomain.com/profile.php?username=XXX http://mydomain.com/profile.php?username=XXX 但问题是我想要的恰恰相反:

当我编写URL时:

http://www.mydomain.com/profile.php?username=XXX http://www.mydomain.com/profile.php?username=XXX 我想重定向到:

http://XXX.mydomain.com http://XXX.mydomain.com 我尝试了所有的可能性,但没有成功

有人能帮我吗?

试试这个

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

那再也不行了。我希望“www”代表所有的URL,但不代表以下内容:[您的代码将URL重定向:[到][@Eli它实际上做的恰恰相反!你的.htaccess中还有其他规则吗?因为我的规则不适用于
sub.mydomain.com
。你能发布你的.htaccess吗?当然可以:SetEnv PHP\u VER 5\u 3 SetEnv REGISTER\u GLOBALS 0 Options+FollowSymLinks RewriteEngine On RewriteBase/RewriteCond%{HTTP_HOST}^(.+).mydomain.com$RewriteRule^(.*)$[R,L]@Eli我认为你发布的内容与我的答案不匹配。RewriteCond和RewriteRule都与我在答案中发布的内容不匹配。你必须更改规则以确保它们有效!这是我的原创。htaccess但通过测试你的,我已删除了我的RewriteCond和RewriteRule。我的答案有帮助吗?你的答案有效吗?