通配符子域mod-rewrite

时间:2013-10-08 05:48:05

标签: php apache .htaccess mod-rewrite wildcard-subdomain

我有一个分配给public_html /的通配符子域*.domain.com

我想将目录www.domain.com/folder1/index.php?name=rock设为rock.domain.com

至于另一个,我想将www.domain.com/folder1/folder2/index.php?id=5改为5.domain.com

有没有办法做到这一点?我是mod-rewrite的初学者。非常感谢您的帮助。感谢

其他信息

我需要他们两个。他们会有不同的变数。

例如,/ folder1 / index.php基于州名(?state = statename)。

对于/folder1/folder2/index.php,它将基于唯一名称(?name = uniquename)。

因此,www.domain.com/folder1/index.php?state=statename将为statename.domain.com

和www.domain.com/folder1/folder2/index.php?name=uniquename将是uniquename.domain.com

谢谢

2 个答案:

答案 0 :(得分:0)

在文档根目录中的htaccess文件中,您可以添加特定于" rock"的规则。和" 5":

RewriteEngine On

RewriteCond %{HTTP_HOST} ^rock\.domain\.com$ [NC]
RewriteRule ^$ /folder1/index.php?name=rock [L,QSA]

RewriteCond %{HTTP_HOST} ^5\.domain\.com$ [NC]
RewriteRule ^$ /folder1/folder2/index.php?name=5 [L,QSA]

如果您希望它向另一个方向重定向,那么您需要:

RewriteEngine On

RewriteCond %{QUERY_STRING} ^state=(.*)$ 
RewriteRule ^folder1/index\.php$ http://%1.domain.com/? [L,R=301]

RewriteCond %{QUERY_STRING} ^name=(.*)$ 
RewriteRule ^folder1/folder2/index\.php$ http://%1.domain.com/? [L,R=301]

答案 1 :(得分:0)

遵循通用规则应该适合您:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+folder1/index\.php\?state=([^\s&]+) [NC]
RewriteRule ^ http://%1.domain.com/? [R=301,L]

RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+folder1/folder2/index\.php\?name=([^\s&]+) [NC]
RewriteRule ^ http://%1.domain.com/? [R=301,L]