Forum Moderators: phranque

Message Too Old, No Replies

append url parameters to url

         

kevinknight

12:49 am on May 9, 2006 (gmt 0)

10+ Year Member



Hi all,
I am trying to park many domains to one folder of my main domain. When a visitor type
[example1.com,...] then I like to append url parameter like [example1.com?site_id=1...]

If another person type [example2.com,...] then it should be [example2.com?site_id=2...]
All those domains will be parked in my one main domain's subfolder.
I am trying to use htaccess and still don't get it right. That url parameter(site_id) should be come along with other urls in corresponding domains, for example, [example1.com...] .
Currently, this works for testing with one domain,

RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)\.php$ /$1.php?site_id=1 [QSA,L,NE,R]

But when I go to other pages, I lost site_id parameter.
Please advise.
Thanks,
Kevin

[edited by: jatar_k at 4:13 pm (utc) on May 9, 2006]
[edit reason] swapped to example [/edit]

RedAndy

2:09 am on May 10, 2006 (gmt 0)

10+ Year Member



Hi Kevin,

you'll need to rewrite all of the URLs within the page to have?site_id=blahblah on the end of them. You could certainly to it with php or another scripting language, but I'm not sure that apache will handle that for you.

Unless you can read the ( unreliable ) HTTP_REFERER on subsequent requests and get the query string out of that to reuse?

hth,

Andy

jdMorgan

1:57 pm on May 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Once you've got the site_id, you don't want to re-run the rule. So you need to test for that.

RewriteEngine On
RewriteCond %{QUERY_STRING} !site_id=.
RewriteCond %{HTTP_HOST} !^(www\.)?main_domain\.com
RewriteCond %{HTTP_HOST} ^(www\.)?([^:]+)
RewriteRule ^([\.]+\.php)$ /$1?site_id=%2 [QSA,L]

Note that the above code puts the domain name into "site_id", rather than trying to associate domains with id numbers one-by-one. You can build an associative list using mod_rewrite if you really feel you must, but it would be much more efficient to handle that once you get into the PHP script.

A simple example of an associative list in mod_rewrite might be:


RewriteEngine On
RewriteCond %{QUERY_STRING} !site_id=.
RewriteCond %{HTTP_HOST}<>2 ^(www\.)?domaintwo\.com[^<]+<>(2) [OR]
RewriteCond %{HTTP_HOST}<>3 ^(www\.)?domainthree\.com[^<]+<>(3) [OR]
RewriteCond %{HTTP_HOST}<>4 ^(www\.)?domainfour\.net[^<]+<>(4) [OR]
RewriteCond %{HTTP_HOST}<>5 ^(www\.)?domainred\.com[^<]+<>(5) [OR]
RewriteCond %{HTTP_HOST}<>6 ^(www\.)?domainblue\.com[^<]+<>(6)
RewriteRule ^([\.]+\.php)$ /$1?site_id=%2 [QSA,L]

Note that the "<>" character sequence has no special meaning whatsoever. I used it simply to distinguish the boundary between the domain and the associated id. Any sufficiently-unique character or characters can be used to do this, as long as they have no special meaning as a regular-expressions operator.

Jim

kevinknight

2:55 pm on May 10, 2006 (gmt 0)

10+ Year Member



Thanks a lot to both of you. Yes, I think I should do it with php script. Get the domain name with $_SERVER[HTTP_HOST] and redirected to www.thatdomain.com/?site_id=#*$!. I can put the domain names and their site ids in database. It will work , I think.
Thanks again.
Kevin