Solved

rewriting rule for sub domains

Posted on 2006-11-19
11
192 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
  • 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
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
 

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

As Wikipedia explains 'robots.txt' as -- the robot exclusion standard, also known as the Robots Exclusion Protocol or robots.txt protocol, is a convention to prevent cooperating web spiders and other web robots from accessing all or part of a websit…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now