Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 233
  • Last Modified:

rewriting rule for sub domains

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
yarek
Asked:
yarek
  • 4
  • 2
  • 2
  • +1
1 Solution
 
giltjrCommented:
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
 
yarekAuthor Commented:
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
 
www-i-p-muCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
yarekAuthor Commented:
This is almost good.. except that ANYTHING is a dynamic parameter !
it can be:

http://blog1.mysite.com
http://myblog.mysite.com
etc...
0
 
giltjrCommented:
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
 
rhickmottCommented:
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
 
giltjrCommented:
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
 
rhickmottCommented:
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
 
giltjrCommented:
Thanks.  I got confused on something and was looking at things backwards.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now