Solved

rewriting rule for sub domains

Posted on 2006-11-19
11
185 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
Comment Utility
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
Comment Utility
0
 
LVL 3

Expert Comment

by:www-i-p-mu
Comment Utility
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
Comment Utility
This is almost good.. except that ANYTHING is a dynamic parameter !
it can be:

http://blog1.mysite.com
http://myblog.mysite.com
etc...
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 57

Expert Comment

by:giltjr
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks.  I got confused on something and was looking at things backwards.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Join & Write a Comment

If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 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

7 Experts available now in Live!

Get 1:1 Help Now