Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

php - Wildcard subdomain mod-rewrite

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...