问题
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