Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
613 views
in Technique[技术] by (71.8m points)

.htaccess - Dynamic subdomain with htaccess (not redirect)

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 !!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...