URL ReWrite SubDomains

ISAPI_Rewrite is Apache mod_rewrite compatible URL rewriter for Microsoft IIS
User avatar
Posts: 4
Joined: 13 Apr 2014, 17:30

URL ReWrite SubDomains

13 Apr 2014, 17:46

Is it possible to
Rewrite
http://subdomain.mydomain.com/..
to http://mydomain.com/subdomain/..

and
http://(www).yourdomain.com/..(with or without www)
to http://mydomain.com/yourdomain-com/...

Query strings and virtual directories will pass along

Thank you

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: URL ReWrite SubDomains

13 Apr 2014, 23:06

Hello,

Sure, try something like:

Code: Select all
RewriteEngine on
RewriteBase /

RewriteCond %{HTTP:Host} ^(?:www\.)?yourdomain\.com$
RewriteRule (.*) http://mydomain.com/yourdomain-com/$1 [NC,R=301,L]

RewriteCond %{HTTP:Host} !^www\.yourdomain\.com$
RewriteCond %{HTTP:Host} ^(.+)\.yourdomain\.com$
RewriteRule (.*) http://mydomain.com/%1/$1 [NC,R=301,L]


Regards

User avatar
Posts: 4
Joined: 13 Apr 2014, 17:30

Re: URL ReWrite SubDomains

14 Apr 2014, 11:42

Actually I meant "yourdomain" could be any other domain
So, anysubdomain will be redirected to mydomain.com/anysubdomain

anyotherdomain.anyextention will be redirected to mydomain.com/anyotherdomain-anyextention

Thank you

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: URL ReWrite SubDomains

14 Apr 2014, 16:54

Would you shed some light here? How many sites do you have to redirect from? Maybe a mapfile is better idea.

Regards

User avatar
Posts: 4
Joined: 13 Apr 2014, 17:30

Re: URL ReWrite SubDomains

15 Apr 2014, 14:15

We provide domain web hosting (Mapfile is not an option)
We would like to rewrite a URL such as
abc.tv to mydomain.com/abc-tv
the domain name and extension are variables
and
abc.mydomain.com to mydomain.com/abc

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: URL ReWrite SubDomains

16 Apr 2014, 09:37

In this case lets try:

Code: Select all
RewriteCond %{HTTP:Host} !^www\.mydomain\.com$ [NC]
RewriteCond %{HTTP:Host} ^(.+)mydomain\.com$
RewriteRule (.*) http://mydomain.com/%1 [NC,R=301,L]

RewriteCond %{HTTP:Host} !^(?:www\.)?mydomain\.com$ [NC]
RewriteCond %{HTTP:Host} ^(?:www\.)?([^.]+)\.([^.]+)$
RewriteRule (.*) http://mydomain.com/%1-%2 [NC,R=301,L]

User avatar
Posts: 4
Joined: 13 Apr 2014, 17:30

Re: URL ReWrite SubDomains

20 Apr 2014, 15:05

I tried the code but it did not do what I am looking for
1- the browser URL changes to mydomain.com/abc-tv (similar to redirect). I would like to keep the URL the same (abc.tv) but IIS serves the new URL (mydomain.com/abc-tv). The redirect should be hidden from the user
2- Also, the code does not pass/keep the subfolder and parameters. For example, for http://www.abc.tv/mydir/default.aspx?id=2, IIS should serves http://www.mydomain.com/abc-tv/mydir/default.aspx?id=2

Thank you

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: URL ReWrite SubDomains

20 Apr 2014, 22:55

1. In this case use [P] instead of [R=301].
2. USe:
Code: Select all
RewriteCond %{HTTP:Host} !^www\.mydomain\.com$ [NC]
RewriteCond %{HTTP:Host} ^(.+)mydomain\.com$
RewriteRule (.*) http://mydomain.com/%1/$1 [NC,P,L]

RewriteCond %{HTTP:Host} !^(?:www\.)?mydomain\.com$ [NC]
RewriteCond %{HTTP:Host} ^(?:www\.)?([^.]+)\.([^.]+)$
RewriteRule (.*) http://mydomain.com/%1-%2/$1 [NC,P,L]

User avatar
Posts: 3
Joined: 29 Apr 2014, 18:04

Re: URL ReWrite SubDomains

29 Apr 2014, 19:15

Sorry to post a reply to an existing thread but my case is very similar.

I already have an existing rule

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


I basically need to get the subdomain as a parameter for every page requested, i will process the id on the top of every page so the above rules works fine only for the root, but it is not grabbing anything after the .com
for example firefox.mysite.com/auth/admin/login.aspx
it should be processed in mysite.com/admin/login.aspx

1) How can I modify the rule to grab the pages after the .com?

2) if the subdomain is empty or starts with www|ftp|mail.mysite.com redirect to http://www.mastersite.com

Can you please help!? thanks

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: URL ReWrite SubDomains

30 Apr 2014, 16:21

Try:

RewriteCond %{HTTP_HOST} !^(?:www|ftp|mail)\.mysite.com
RewriteCond %{HTTP_HOST} ^(.+).mysite.com
RewriteRule ^([^/]*)$ http://mysite.com/%1/$1 [P,L]

To debug, I'd suggest using R=301 instewad of P-flag. In this case URL would change and you'd actually see the landing URL.

Regards

User avatar
Posts: 3
Joined: 29 Apr 2014, 18:04

Re: URL ReWrite SubDomains

30 Apr 2014, 16:53

Hi Andrew thanks for your reply, rule doesn't work as I expected.

I need to be able to pass control to whatever page they are trying to hit

for example if they hit http://helicon.mysite.com/auth/login.asxp

I need the rule to hit http://mysite.com/auth/login.aspx?id=helicon

Note:* i dont need to add the subdomain to the url just as a parameter.

The below rule works well only if I hit the root

Code: Select all
RewriteCond %{HTTP_HOST} !^(?:www|ftp|mail)\.mysite.com
RewriteCond %{HTTP_HOST} ^(.+).mysite.com
RewriteRule ^([^/]*)$ http://mysite.com/$1?id=%1 [P,L]



Please advise.

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: URL ReWrite SubDomains

30 Apr 2014, 17:02

In this case you'll end up with
either
Code: Select all
RewriteCond %{HTTP_HOST} !^(?:www|ftp|mail)\.mysite.com
RewriteCond %{HTTP_HOST} ^(.+).mysite.com
RewriteRule ^(.*)$ http://mysite.com/$1?id=%1 [P,L]

or
Code: Select all
RewriteCond %{HTTP_HOST} !^(?:www|ftp|mail)\.mysite.com
RewriteCond %{HTTP_HOST} ^(.+).mysite.com
RewriteRule ^(.*)$ http://mysite.com$1?id=%1 [P,L]

User avatar
Posts: 3
Joined: 29 Apr 2014, 18:04

Re: URL ReWrite SubDomains

30 Jun 2014, 22:27

Hi Andrew, Sorry but we need your help one more time.

We have the following rule:

Code: Select all
RewriteCond %{HTTP_HOST} !^(?:www|ftp|mail)\.mysite.com
RewriteCond %{HTTP_HOST} ^(.+).mysite.com
RewriteRule ^(.*)$ http://mysite.com/$1?id=%1 [P,L]


Which works fine, if someone hits http://subdomain.mysite.com
it gets the subdomain as a parameter = http://mysite.com/id=subdomain.

However any existing querystring is being ignored.

for example if I hit
http://subdomain.mysite.com/order.aspx?cart=abc

I never get the cart value, the Querystring is gone. I only get the id=subdomain.

Does it make sense?

Please advise!

Return to ISAPI_Rewrite 3.0

Who is online

Users browsing this forum: No registered users and 15 guests