You are here: Re: rewrite for sub-domains... « All PHP « IT news, forums, messages
Re: rewrite for sub-domains...

Posted by Christoph Burschka on 12/06/06 08:29

Kentor schrieb:
> Hello, I know how to use rewrite on my main domain page but when it
> comes to sub-domains it just doesn't work. I'm trying to have the url:
> http://montreal.mysite.com show the page that is shown by
> www.mysite.com/searchForm.php?location=Montreal and another
> subdomain like http://www.toronto.mysite.com show the page for
> www.mysite.com/searchForm.php?location=Toronto
> thanks for your help.
>
mod_rewrite doesn't work across sub-domains, as far as I know - at least
not unless you use the [R] flag which redirects the user to the URL you
specify.

But that means the user will actually receive a redirect response and
will see the new URL in their address bar - if you are using mod_rewrite
for clean URLs, this probably isn't what you want.

--

Here's an idea: I assume you have a wild-card DNS entry for
*.mysite.com, so all these subdomains point to the same folder? If so,
does www.mysite.com also point to this folder?

If so, this might work. First, put this rewrite in your .htaccess:

RewriteCond %{HTTP_HOST} (.+)\.mysite.com # checks for sub-domain
RewriteCond %{HTTP_HOST} !='www.mysite.com' # www doesn't count
RewriteRule ^/.*$ /searchForm.php [L,QSA]

This will rewrite all requests for *.mysite.com to the searchForm.php
page (preserving query strings, so if you have any additional parameters
these will be transferred as well).

Then, add this to the beginning of searchForm.php :

<?php
if (preg_match('/^(.*)\.mysite.com$/',$_SERVER['HTTP_HOST'],$match) {
if ($match[1]!='www') $_GET['location']=$match[1];
}
?>

This will check whether the page was called with *.mysite.com and, if it
was, move the subdomain over to the location parameter.

The search form will then work as if it had been called with
searchForm.php?location=*.

Note that this will only work if the subdomains are all served from the
same folder on the server; that is, if it would also be possible to
visit this URL:

http://toronto.mysite.com/searchForm.php

If it doesn't, you need to change that - or at least copy the search
form over to the other folder.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация