htaccess子域为get变量

时间:2014-01-18 15:28:59

标签: php apache .htaccess mod-rewrite subdomain

我想这样做。

user.mydomain.com -> mydomain.com/user.php?user=user
user.mydomain.com/content_(.*).xhtml -> mydomain.com/user.php?user=user&content=(.*)
user.mydomain.com/content_(.*).xhtml?get=(.*) ->mydomain.com/user.php?user=user&content=(.*)&get=(.*)

我已经尝试过这个

<Directory "/var/www/html">

RewriteEngine On


# host doesn't start with www
RewriteCond %{HTTP_HOST} !^www\.  [NC]
RewriteCond %{HTTP_HOST} !^m\.  [NC]

# host starts with something else
RewriteCond %{HTTP_HOST} ^([^\.]+)\.mydomain\.com$  [NC]

# rewrite
RewriteRule ^(.*)$ users.php?user=%1  
RewriteRule ^users\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^content_(.*).xhtml$ users.php?user=%1&content=$1 [L,QSA] 
</Directory>

但它只传递子域而不是内容参数值。

1 个答案:

答案 0 :(得分:0)

DocumentRoot/.htaccess

中使用此代码
RewriteEngine On

RewriteRule ^users\.php$ - [L]

RewriteCond %{HTTP_HOST} !^(?:www|m)\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com$  [NC]
RewriteRule ^$ /users.php?user=%1 [L,QSA]

RewriteCond %{HTTP_HOST} !^(?:www|m)\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com$  [NC]
RewriteRule ^content_([^.]+)\.xhtml$ /users.php?user=%1&content=$1 [L,QSA,NC]