Dynamic subdomain with htaccess (not redirect)

萝らか妹 提交于 2019-11-27 12:36:16

问题


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:


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]


来源:https://stackoverflow.com/questions/13875273/dynamic-subdomain-with-htaccess-not-redirect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!