Dynamic subdomain with htaccess (not redirect)

2019-01-22 10:03发布

Currently, I'm having an url system like this by using the htaccess:

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

.. something like that

Here is my htaccess, and it worked perfectly.

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

Now I want to setup a dynamic subdomain system for the user, likes "username.domain.com" So I decided to used the below htaccess:

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

But this redirect the user to their old domain "www.domain.com/member/username" instead of "www.domain.com/member/username" I want to keep the user stay in "username.domain.com" (no url change in the address bar).

If possible, is there any chance to keep the same structure for other new url, such as when I type:

  • "username.domain.com/contact" will load the content of "www.domain.com/member/username/contact" (no url change in the address bar)
  • "username.domain.com/album/title-of-the-album/albumID" will load the content of "www.domain.com/member/username/album/title-of-the-album/albumID" (no url change in the address bar)

Please help !!

1条回答
男人必须洒脱
2楼-- · 2019-01-22 10:46

Try:

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]
查看更多
登录 后发表回答