Create Subdomain from Directory

Topics: Developer Forum
Jun 26, 2008 at 3:22 PM
Hi,
    I am getting problem, while rewriting URL.
   
    When user try to acceess url "http://subdomain.domain.com", It should rewrite to "http://domain.com/subdomain/"
    "http://subdomain.domain.com  ==>>     http://domain.com/subdomain/

    Please suggest working RewriteCond or RewriteRule.
   
    How can we archive this using IIRF.
    Please reply..

- Kartik



   
Jun 26, 2008 at 3:24 PM
I tried

RewriteLogLevel 3
RewriteCond %{HTTP URL} ^([^.]+)\.domain\.com
RewriteRule (.*) /%1/$1 [L]

But its not working..
Coordinator
Jun 26, 2008 at 4:27 PM
Edited Jun 26, 2008 at 4:28 PM

"It's not working"

Can you show us the log file?

 

Jun 27, 2008 at 6:33 AM
I tried

RewriteLogLevel 3
RewriteLog c:\kartik\IirfLog
RewriteCond %{HTTP_HOST} !^www\.two\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.two\.com
RewriteRule (.*) /%1/$1 [L]


which is working in ISAPI_Rewrite 3.0

as log generated while change ini file is

Fri Jun 27 11:58:52 2008 - AwaitIniChangeAndReinit: Detected change in the  ini file 'C:\Kartik\IsapiRewrite4.ini'
Fri Jun 27 11:58:52 2008 - AwaitIniChangeAndReinit: Ionic ISAPI Rewriting Filter (IIRF) v1.2.14 final
Fri Jun 27 11:58:52 2008 - ReadConfig
Fri Jun 27 11:58:52 2008 - ReadConfig: setting LogLevel to 3
Fri Jun 27 11:58:52 2008 - LogFile re-opened.
Fri Jun 27 11:58:52 2008 - ReadConfig: new log file name: 'c:\kartik\IirfLog.492.log'
Fri Jun 27 11:58:52 2008 - ReadConfig: WARNING: log file is under the ini file directory. Don't do this! Check the readme.
Fri Jun 27 11:58:52 2008 - ReadConfig: line   3: RewriteCond %{HTTP_HOST}                                   !^www\.two\.com$                         
Fri Jun 27 11:58:52 2008 - ParseCondModifierFlags: '[NC]'
Fri Jun 27 11:58:52 2008 - WARNING: unsupported RewriteCond modifier flag 'NC'
Fri Jun 27 11:58:52 2008 - ReadConfig: line   4: RewriteCond %{HTTP_HOST}                                   ^([^.]+)\.two\.com                       
Fri Jun 27 11:58:52 2008 - ReadConfig: line   5: RewriteRule   1 (.*)                                           /%1/$1                                          [L]
Fri Jun 27 11:58:52 2008 - ParseRuleModifierFlags: '[L]'
Fri Jun 27 11:58:52 2008 - ReadConfig: Done reading, found 1 rules (0 failed) on 6 lines

--------------------------------------------
Fri Jun 27 11:58:52 2008 - LogFile re-opened.


and when try to execute page genarate

Fri Jun 27 12:01:31 2008 - HttpFilterProc: SF_NOTIFY_URL_MAP
Fri Jun 27 12:01:31 2008 - HttpFilterProc: SF_NOTIFY_AUTH_COMPLETE
Fri Jun 27 12:01:31 2008 - DoRewrites
Fri Jun 27 12:01:31 2008 - GetServerVariable_AutoFree - no joy (GetLastError()=1413)
Fri Jun 27 12:01:31 2008 - DoRewrites: New Url: '/'
Fri Jun 27 12:01:31 2008 - ApplyRules: depth=0
Fri Jun 27 12:01:31 2008 - ApplyRules: Rule 1 : 2 matches
Fri Jun 27 12:01:31 2008 - EvalCondition: ts1 'domain1.two.com'
Fri Jun 27 12:01:31 2008 - ApplyCaseConversion: after  'domain1.two.com'
Fri Jun 27 12:01:31 2008 - EvalCondition: checking 'domain1.two.com' against pattern '!^www\.two\.com$'
Fri Jun 27 12:01:31 2008 - EvalCondition: match result: -1 (No match)
Fri Jun 27 12:01:31 2008 - EvalConditionList: FALSE, Rule does not apply
Fri Jun 27 12:01:31 2008 - ApplyRules: returning 0
Fri Jun 27 12:01:31 2008 - DoRewrites: No Rewrite
Fri Jun 27 12:01:31 2008 - HttpFilterProc: SF_NOTIFY_URL_MAP
Fri Jun 27 12:01:31 2008 - HttpFilterProc: SF_NOTIFY_URL_MAP
Fri Jun 27 12:01:31 2008 - HttpFilterProc: SF_NOTIFY_URL_MAP


Please give me exact rewritecond and rewriterule, if this is wrong..

Thanks for replay...


Coordinator
Jun 27, 2008 at 2:48 PM

Why not just something like this:

RewriteCond %{HTTP_HOST} ^(?!www)([^.]+)\.dinoch\.net
RewriteRule ^/(.*)$ /%1/$1 [L] 

This says:  for any HTTP_HOST that matches *.dinoch.net but does not begin with www, fire the rewrite rule. 
The rule says, for any URL at all - rewrite to a URL that contains the first segment of the HTTP_HOST. 

And remember that you can detect the existence of subdirectories in the filesystem with IIRF. See the -d flag in the readme.

One last thing: your log file says:

WARNING: log file is under the ini file directory. Don't do this! Check the readme.

You should follow that advice.

Jul 21, 2008 at 11:39 AM
Thanks for reply....

I tried your solution..but it didnt worked. The log for it.

Mon Jul 21 17:04:58 2008 - LogFile re-opened.
Mon Jul 21 17:05:17 2008 - HttpFilterProc: SF_NOTIFY_URL_MAP
Mon Jul 21 17:05:17 2008 - HttpFilterProc: SF_NOTIFY_AUTH_COMPLETE
Mon Jul 21 17:05:17 2008 - DoRewrites
Mon Jul 21 17:05:17 2008 - GetServerVariable_AutoFree - no joy (GetLastError()=1413)
Mon Jul 21 17:05:17 2008 - DoRewrites: New Url: '/'
Mon Jul 21 17:05:17 2008 - ApplyRules: depth=0
Mon Jul 21 17:05:17 2008 - ApplyRules: Rule 1 : 2 matches
Mon Jul 21 17:05:17 2008 - EvalCondition: ts1 'domain1.two.com'
Mon Jul 21 17:05:17 2008 - ApplyCaseConversion: after  'domain1.two.com'
Mon Jul 21 17:05:17 2008 - EvalCondition: checking 'domain1.two.com' against pattern '^(?!www)([^.]+)\.two\.com'
Mon Jul 21 17:05:17 2008 - EvalCondition: match result: 2 (match)
Mon Jul 21 17:05:17 2008 - EvalConditionList: TRUE, Rule will apply
Mon Jul 21 17:05:17 2008 - ApplyCaseConversion: after  '/%1/'
Mon Jul 21 17:05:17 2008 - ApplyRules: Result (length 4): /%1/
Mon Jul 21 17:05:17 2008 - ApplyRules: Last if Match
Mon Jul 21 17:05:17 2008 - ApplyRules: returning 1
Mon Jul 21 17:05:17 2008 - DoRewrites: Rewrite Url to: '/%1/'
Mon Jul 21 17:05:17 2008 - HttpFilterProc: SF_NOTIFY_URL_MAP

Its resolve only home page of site...please give any idea...
Coordinator
Jul 22, 2008 at 12:19 AM

Yes there is a bug.  Please add te following line to your INI file:

   CondSubstringBackrefFlag  %

Also, there were two minor fixes added to v1.2.14 after the "final" release. 
You can go to the Releases tab and re-download the release to get them. 
(This was one of the fixes.  If you have the fix, then you do not need the CondSubstringBackrefFlag   directive.)

Jul 22, 2008 at 6:09 AM
Edited Jul 22, 2008 at 6:09 AM
Hey...Cheeso,
   Thank you very much....now its working fine.

Keep it up
Keep Smiling...

-Kartik

Aug 21, 2008 at 2:09 PM
hello there,


similar question ..

http://subdomain.domain.com     to    http://domain.com/folder/subdomain/en/


i have no testing enviroment now, because i am in holidays, but please can you tell if this will work:

RewriteCond %{HTTP_HOST} ^(?!www)([^.]+)\.domain\.com <o:p></o:p>
RewriteRule ^/(.*)$ /%1 \folder\ /$1 \en\ [L] <o:p></o:p>

CondSubstringBackrefFlag  %<o:p></o:p>


thx

Coordinator
Aug 21, 2008 at 4:51 PM

no.

If you want to change domains, then you want a redirect, which implies the use of the [R] flag.  It also means you need to specify the full URL of the redirect.
So, something like this:

CondSubstringBackrefFlag  %

RewriteCond %{HTTP_HOST} ^(?!www)([^.]+)\.domain\.com 
RewriteRule ^/(.*)$    http://domain.com/folder/%1/en/$1 [R]

I don't know exactly what you want with the redirect URL - whether "folder" and "en" are literals or not. But maybe you get the idea.

Aug 22, 2008 at 6:57 AM
i will explain my question above:


we have the domain www.XXXhotels.gr and we show the recommended hotels under www.XXXhotels.gr/recommended/hotelname/en (en stands for english).

we also have subdomains for each recommended hotel:     hotelname.XXXhotels.gr

i want to make it possible to our clients to enter the URL for the subdomain, redirect them to the real Location, but i want to show them the subdomain URL in the browser.


sorry for my english.

thanks
Coordinator
Aug 23, 2008 at 3:17 PM

Ok, then you do not want a redirect. You want a rewrite. try this?

CondSubstringBackrefFlag  %

RewriteCond %{HTTP_HOST} ^(?!www)([^.]+)\.XXXhotels\.gr 
RewriteRule ^/(.*)$   /recommended/%1/en/$1

keep in mind: this is a starting point. You will have to try it out, make some changes in the rules, analyze the log files, and see what works for you.

good luck.

Apr 14, 2009 at 7:42 AM
Edited Apr 14, 2009 at 7:42 AM
Hey,
	I wrote same as u suggested me before. 
CondSubstringBackrefFlag  %
RewriteCond %{HTTP_HOST} ^(?!www)([^.]+)\.domain\.com
RewriteRule ^/(.*)$ /%1/$1 [L] 
It is working fine if request comes from http://subdomain.domain.com
But will failed with request http://www.subdomain.domain.com
Can you give me suggestion to solve this problem?