Solved

rewriting rule for sub domains

Posted on 2006-11-19
11
215 Views
Last Modified: 2010-03-04
I have a blog script and a DNS for my website put on *
so:   ANYTHING.mysite.com leeds to ->mysite.com/index.php when I retrieve the subdomain (which is ANYTHING ) and I redirect it to

http://www.mysite.com/ANYTHING


this is the first step (I have suceed this one)

Now I would like to keep the url http://ANYTHING.mysite.com instead of http://mysite.com/ANYTHING

I think it should be (easily) achieved by APACHE URL REWRITING.
Any solutions for this ?

thanks


0
Comment
Question by:yarek
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
11 Comments
 
LVL 57

Expert Comment

by:giltjr
ID: 17976141
Does each host name have a unique Web Page?  Do you know what "ANYTHING" is before or can it be literally anything and you expect it to work.

Are you looking to replace the index.php function?

Would dynamic virtual hosting be better?  

     http://httpd.apache.org/docs/2.0/vhosts/mass.html

This does requre you to know what "ANYTHING" is before hand.
0
 

Author Comment

by:yarek
ID: 17977580
infact :

http://www.mysite.com/ANYTHING/webpage.php?param1=xxx¶m2=yyy...¶mM=zzz

should show
http://ANYTHING.mysite.com/webpage.php?param1=xxx¶m2=yyy...¶mM=zzz


0
 
LVL 3

Expert Comment

by:www-i-p-mu
ID: 17987666
try to use this .htaccess file:

AddHandler cgi-script .html

RewriteEngine on
# Http-host is not...
RewriteCond %{HTTP_HOST} !^www\.mysite\.com
# ..and is not empty...
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*) http://ANYTHING.mysite.com/$1 [R=301,L]

Hope this is useful fo you,

Regards
0
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 

Author Comment

by:yarek
ID: 17987700
This is almost good.. except that ANYTHING is a dynamic parameter !
it can be:

http://blog1.mysite.com
http://myblog.mysite.com
etc...
0
 
LVL 57

Expert Comment

by:giltjr
ID: 17988179
I have a question.  If "ANYTHING" is truly dynamic, what do you expect to display?  I would assume that if "ANYTHING" is unknown that you must display the same thing for all unknowns.

If I were to go to http://name1.mysite.com and to http://name2.mysite.com what do you display?

0
 
LVL 13

Accepted Solution

by:
rhickmott earned 500 total points
ID: 17988468
This looks familiar :/

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.mysite\.com$ [NC]
RewriteCond %{HTTP_HOST} !^mail\.mysite\.com$ [NC]
RewriteCond %{HTTP_HOST} !^$ [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.mysite\.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/%1/$1 [R=301,L]

Ill repost this I cant test it as I don't have any sub domains on my test rigs. Basically you need to capture the sub domain into a environment value ( %1 ) in this case. You can then use this in your rewrite rule.
0
 
LVL 57

Expert Comment

by:giltjr
ID: 17988521
I would think that you can do this with virtual hosting.  As "ANYTHING" could be unknown, at somepoint in time you have to know what it is in order to direct it someplace unique.  So you could have:

<Virtualhost *:80>
   ServerName known01.mysite.com
</Virtualhost>

<Virtualhost *:80>
   ServerName known02.mysite.com
</Virtualhost>

<Virtualhost *:80>
   ServerName known03.mysite.com
</Virtualhost>

<Virtualhost *:80>
   ServerName www.mysite.com
   ServerAlias mysite.com *.mysite.com
</Virtualhost>

In this situation if I go to http://known01.mysite.com, or known02 or known03, I will get the virtual sites for those hosts.  If I go to http://known04.mysite.com I will get the default for "www.mysite.com".  Once known04 is setup, a new Virtualhost entry will need to be created for it.
0
 
LVL 13

Expert Comment

by:rhickmott
ID: 17988607
That is the whole point in URL rewriting :)

The user obv has a % subdomain on their main domain record. That means anything.domain.com will hit their server. even if they uses your virtualhosts above it will would go to

<Virtualhost *:80>
   ServerName www.mysite.com
   ServerAlias mysite.com *.mysite.com
</Virtualhost>

This record as it specifies a *. record on the domain.

If its not explicitly names in their Apache config then their server WILL handle it in some way usually by supplying the first site or virtual host it comes across. If you have 3 domains pointers at a server and handle 2 normally the third will bring up the first.

by using Mod Rewrites the user can rewrite an incoming url and redirect it it does not NEED to be known because a request for domainxyz.mydomain.com will call the document root for mydomain.com as it doesent know how to deal with it. By placing a rewrite rule in the HTTP.CONF or in a .HTACCESS file in the documebnt root for *.domain.com it will redirect a request from domainxyz.mydomain.com to www.mydomain.com/domainxyz/

If the folder exists the user will see the new address in their bar and everything is fine of not they will get a 404 either way the result is as intended.

Its the same way people have a content system where they have one script which pulls pages from a database based on page.php?page=news they will set up a request so that x.html grabs page.php?page=x.

This is so they can have search engine friendly pages.

0
 
LVL 57

Expert Comment

by:giltjr
ID: 17988810
Thanks.  I got confused on something and was looking at things backwards.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you are running a LAMP infrastructure, this little code snippet is very helpful if you are serving lots of HTML, JavaScript and CSS-related information. The mod_deflate module, which is part of the Apache 2.2 application, provides the DEFLATE…
If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question