Pass subdomain as parameter

北城余情 提交于 2019-12-12 18:29:20

问题


I'm new to ISAPI_Rewrite & I'm terrible with regular expressions. I'm in need of a rewrite rule for ISAPI_Rewrite that will remove the subdomain and pass it as a parameter. For example:

mysubdomain.mydomain.com

should become

mydomain.com/Landing.aspx?ID=mysubdomain

I've found a regex that seems to match all subdomains except www, but I'm not sure how to then pass the subdomain as a parameter as the example above shows.

^((?:(?!www).)*)

Any help would be appreciated.

Note: I'm using the full version of ISAPI_Rewrite so this rule will be at the site-level.


Global rule:

# Helicon ISAPI_Rewrite configuration file
# Version 3.1.0.89

#Disable extentionless processing for ASP.Net v 4.0
RewriteRule (.*)eurl.axd/.* $1

# Don't rewrite urls that are inside the assets folder or have the following extentions
RewriteRule ((^/Assets/.*)|(.*\.axd.*)|(.*\.asmx.*)|(.*\.png.*)) $1 [NC,L]

#Rewrite URL, pass last portion of URL to Landing.aspx + purl
RewriteRule  (.*) $2/landing.aspx\?id=$1 [NC]

回答1:


Something like this may work for you:

# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} ^$
RewriteCond %{HTTP_HOST} ([^.]+)(?<!^www)(\.|$) [NC]
RewriteRule ^ Landing.aspx?ID=%1 [L,QSA]

PS: Looks like your global rule is in conflict with above rule of mine. I would therefore suggest you to have your global rule like this to avoid conflict:

RewriteCond %{QUERY_STRING} ^$
RewriteCond %{HTTP_HOST} !mydomain\.com$ [NC]
RewriteRule ^(.*)$ landing.aspx?id=$1 [L]



回答2:


Try using the following:

RewriteEngine on
RewriteBase /

RewriteCond %{HTTP:Host} !^www\.mydomain\.com$
RewriteCond %{HTTP:Host} ^([^.]+)\.mydomain\.com$
RewriteRule ^$ /Landing.aspx?ID=%1 [NC,L]

You did not specify if you want a 301-redirect or a rewrite,so in case you want it to be redirect, use [NC,R=301,L] instead of [NC,L].



来源:https://stackoverflow.com/questions/10738175/pass-subdomain-as-parameter

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