Wildcard subdomain and subfolders as parameters in .htaccess

人盡茶涼 提交于 2019-12-13 14:35:42

问题


I have a portal site http://www.mysite.com/ where customers sign up and they get their own subdomain version of the site to run my app. I've set up the wildcard subdomain DNS/VirtualHost etc and got it working.

What I am trying to set up is my htaccess file to pass the wildcard subdomain as a parameter to the app, like this:

http://wildcardsubdomain.mysite.com/ -> http://www.mysite.com/app/?dj=wildcardsubdomain
http://wildcardsubdomain.mysite.com/?l=genres -> http://www.mysite.com/app/?dj=wildcardsubdomain&l=genres

I accomplished this by having this in my .htaccess file in http://www.mysite.com/

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.mysite.com
RewriteCond %{HTTP_HOST} ^(.+).mysite.com
RewriteRule ^([^/]*)$ http://www.mysite.com/app/dj/%1 [P,L]

But I need to redirect the admin user URLs as follows and I've gotten stuck with the htaccess code to use inside the /app directory. What I need is:

http://wildcardsubdomain.mysite.com/admin/?l=genres -> http://www.mysite.com/app/admin.php?dj=wildcardsubdomain&l=genres
http://wildcardsubdomain.mysite.com/admin/?l=users -> http://www.mysite.com/app/admin.php?dj=wildcardsubdomain&l=users

(the l parameter varies, I've just used users and genres here as an example)

I've tried this inside /app/.htaccess but it hasnt really worked:

RewriteRule ^dj/([^/]+)/?$ $2?dj=$1 [QSA,L]
RewriteRule ^admin/([^/]+)/?$ admin.php$2?dj=$1 [QSA,L]

Can anyone suggest a better rewrite rule? Many thanks in advance


回答1:


You're missing a QSA flag, as the l=genres gets lost otherwise. You just need an "admin" rule before your other rule:

RewriteEngine on

RewriteCond %{HTTP_HOST} !^www\.mysite.com
RewriteCond %{HTTP_HOST} ^(.+).mysite.com
RewriteRule ^admin/?$ http://www.mysite.com/app/admin.php?dj=%1 [P,L,QSA]

RewriteCond %{HTTP_HOST} !^www\.mysite.com
RewriteCond %{HTTP_HOST} ^(.+).mysite.com
RewriteRule ^([^/]*)$ http://www.mysite.com/app/dj=%1 [P,L,QSA]


来源:https://stackoverflow.com/questions/18710886/wildcard-subdomain-and-subfolders-as-parameters-in-htaccess

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