Solved

rewriting rule for sub domains

Posted on 2006-11-19
11
220 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

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). #…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

630 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