Wildcard subdomain mod-rewrite

元气小坏坏 提交于 2019-12-13 07:51:12

问题


I have a wildcard subdomain *.domain.com assigned to public_html/.

I want to make the directory www.domain.com/folder1/index.php?name=rock to rock.domain.com.

As for another one, I want to make www.domain.com/folder1/folder2/index.php?id=5 to 5.domain.com

Are there any way to do this? I'm a beginner in mod-rewrite. Really appreciate your help. Thanks

Additional Information

I need both of them. They will have different variables.

For example, /folder1/index.php is based on state name(?state=statename).

For the /folder1/folder2/index.php, it will be based on unique name(?name=uniquename).

So, www.domain.com/folder1/index.php?state=statename will be statename.domain.com

and www.domain.com/folder1/folder2/index.php?name=uniquename will be uniquename.domain.com

Thank you


回答1:


In the htaccess file in your document root, you can add rules specific for "rock" and "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]

If you want it to redirect in the other direction then you'd need:

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]



回答2:


Following generic rule should work for you:

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]


来源:https://stackoverflow.com/questions/19240015/wildcard-subdomain-mod-rewrite

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