2012-12-14 28 views
4

目前,我有一个网址系统,这样使用htaccess的:与htaccess的动态子域(不定向)

- www.domain.com/member/username 
- www.domain.com/member/username/contact 
- www.domain.com/member/user/album/title-of-the-album/albumID 

..类似的东西

这里是我的htaccess的,它完美地工作。

RewriteRule ^member/([a-zA-Z0-9_-]+)$ member.php?username=$1 
RewriteRule ^member/([a-zA-Z0-9_-]+)/contact$ contact.php?username=$1 
RewriteRule ^member/([a-zA-Z0-9_-]+)/album/([a-zA-Z0-9_-]+)/([0-9]+)$ album.php?username=$1&title=$2&album_id=$3 

现在我想建立一个动态的子域名系统,对于用户来说,喜欢“username.domain.com” 所以我决定用下面的htaccess的:

RewriteCond %{HTTP_HOST} !^www\.domain\.com 
RewriteCond %{HTTP_HOST} ([^.]+)\.domain\.com [NC] 
RewriteRule ^(.*)$ www.domain.com/member/%1 [L] 

但是,这将用户重定向到他们的旧域名“www.domain.com/member/username”,而不是“www.domain.com/member/username” 我想保持用户留在“username.domain.com”(没有URL更改地址栏)。

如果可能的话,有没有可能保持相同的结构等新的URL,这样当我输入为:

  • “username.domain.com/contact”将载入“WWW的内容。 “domain.com/member/username/contact”(地址栏中没有网址变化)
  • “username.domain.com/album/title-of-the-album/albumID”将加载“www.domain .com /会员/用户名/相册/标题的相册/ albumID“(地址栏中没有网址变化)

请帮忙!!

回答

4

尝试:

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

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

RewriteCond %{HTTP_HOST} !^www\.domain\.com 
RewriteCond %{HTTP_HOST} ([^.]+)\.domain\.com [NC] 
RewriteRule ^/?album/([a-zA-Z0-9_-]+)/([0-9]+)$ /album.php?username=%1&title=$1&album_id=$2 [L] 
+0

其worked.However,我必须设置为每一个超链接一样喜欢“/接触”“/专辑”或“/照片”?无论如何,它会自动应用它们吗? – HoanNguyen

+0

@HoanNguyen看起来他们必须分开,因为“contact”和“album”有一组不同的查询字符串(用户名和用户名/标题/相册)。他们必须分开。 –

+0

嗨,我是一个类似的问题,你能看看我的问题吗?谢谢,我的问题是:[问题](http://stackoverflow.com/questions/32133016/dns-error-with-dynamic-subdomains-with-htaccess) – Jjsg08