htaccess - 子域作为GET参数

时间:2012-03-04 01:50:01

标签: apache .htaccess mod-rewrite subdomain

我的.htaccess文件中已有以下内容:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]

但我想添加一个这样的规则:

RewriteCond %{HTTP_HOST} ^(.+).example.com
RewriteRule ^(.*)% http://example.com/?name=%1&type=$1 [R,L]

但如果它没有绑定到example.com,它必须适用于任何域。

1 个答案:

答案 0 :(得分:3)

RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.(.*)\.com [NC]
RewriteRule (.*) http://www.%2.com/?name=%1 [R=301,L]

http://subdomain.domain.com重写为http://www.domain.com/?name=subdomain

将两者结合起来尝试这样的事情

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.(.*)\.com [NC]
RewriteRule (.*) http://www.%2.com/index.php?route=$1&name=%1 [R=301,L]

会将http://subdomain.domain.com/hello-world重定向到http://www.domain.com/index.php?route=hello-world&name=subdomain