Forum Moderators: phranque

Message Too Old, No Replies

rewriting subdomains to a php script with a resource name

         

macdar

9:19 pm on Jul 5, 2005 (gmt 0)

10+ Year Member



Hi,
I have a piece of code with a following structure:

an index.php file with "if" statements:
if($v == "1")
{
$w->someFunction();
include("sometemplate.php"):
}
elseif($v == "2")
{
...
etc..

I need to set up a global variable in the index.php file, which will be passed to all of my methods..
(basically that global variable is supposed to be a prefix for my sql tables)
What I can't get done is that the global variable is not being passed to particular statements, with an except of the last "else" statement..
(I tried to set it up as a global var, tried to use define function.. I have register_globals set to ON..)

maybe it's worth to mention, that the global var is a variable taken out of subdomain using an .htaccess file:
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.domain\.com
RewriteRule ^$ /index.php?variable=%2 [L]

so, I'd like to pass that variable to all of if/elseif statements:

if($v==1){
$variable = "somevalue";
$w->someFunction();
}

Does anyone know how to define a global variable for all if/elseif statements?

Thanks.

mcibor

9:29 pm on Jul 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



to define global variable you need to define it inside the function.
function someFunction() {
global $variable;
}

However I really recommend using reference:
function someFunction($variable) {
$variable = 2;
return "Whatever";}
...
$w->someFunction(&$variable);
...

Moreover you can discard the if/elseif/else structure and change it to switch:

switch ($v) {
case 1: $w->someFunction();
include("sometemplate.php");
break;
case 2: ... break;
}
Best regards
Michal CIbor

macdar

10:03 pm on Jul 5, 2005 (gmt 0)

10+ Year Member



Michal thanks for your quick reply, however I'm not sure if you understood me correctly.

ok, here's the deal:

If you could take a look at the piece of the code of an .htaccess file you will see that a variable called "variable" is being passed to index.php file - and this part works fine.(cause when I type a URL "subdomain.mydomain.com" - the subdomain value is assigned to the "variable"..) - and this, as I said earlier, is a result of calling the "else" statement..

so, the problem is, that when I type, let's say:
subdomain.mydomain.com/index.php?v=2
( which calls elseif ($v=="2"){ some action..} )
then the variable is not passed.. ($variable = "";)

so, looks like the $variable is not being passed along with $v..

So, I assume I'd have to tell php to pass along that variable to all my statements (not to functions, because that's already one step ahead..)

hope that makes sense.

Birdman

10:37 pm on Jul 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think what's happening is that you are losing the original querystring in the rewrite. Try adding the "QSA" flag, which will append the original querystring.

RewriteRule ^$ /index.php?variable=%2 [QSA,L]

Also, I agree with mcibor, that the switch() control is cleaner.

macdar

10:57 pm on Jul 5, 2005 (gmt 0)

10+ Year Member



.htaccess works fine, cause otherwise I would get the
"variable" at all..

/*this gives nothing*/
if ($v == "1") {
$variable = $_GET[variable];
echo $variable;
}
/*this gives nothing as well*/
elseif ($v == "2") {
$variable = $_GET[variable];
echo $variable;
}
/*this gives a value of the variable*/
else
{
$variable = $_GET[variable];
echo $variable;
}

so rewrite is fine, besides that's jatar_k's code..

as far as using switch I don't see anything against using it, but that's not the point here - first I wanna get it working..

willybfriendly

11:14 pm on Jul 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try assigning $v

$v = $_GET[v];

WBF

macdar

11:56 pm on Jul 5, 2005 (gmt 0)

10+ Year Member



Ok,
one more time.. maybe somebody will be able to give me a ligitimate answer..

I want to set up subdomains so that name.domain.com calls a php script at [domain.com...]

in order to do so, I have an .htaccess file which handles that:
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.domain\.com
RewriteRule ^$ /index.php?subdomain=%2 [L]

and..the index.php file also includes whole bunch of if if/elseif statements (which I'm going to change to switch).. depending on what condition you choose you're calling different functions..

I want the variable $subdomain to be some kind of config variable - it's supposed to be a prefix for my sql tables..

so if you choose
[myname.domain.com...]
it selects all data from a table called myname_sqltable or something like that..

if you choose [myname.domain.com...]
id deletes all data from a table called myname_sqltable..

etc..etc..

so, as I said there is plenty of if statements in the index.php file - and I can't pass the $subdomain variable to them..

How to define that subdomain variable so that it will be dynamically changed, depending at what URL you are (subdomain)..?

Has anyone ever had a similar issue?

willybfriendly

4:29 am on Jul 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



SO, if I understand the problem correctly, $v is not getting passed, but the subdomain is correctly being rewritten as a variable. That explains why $subdomain shows in the last elseif. The script falls through the others since $v has not been set.

Look at your rewrite rule again. I am no regex whiz, but I don't see where you are allowing for $v in the rewrite.

RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.domain\.com
RewriteRule ^$ /index.php?subdomain=%2 [L]

I read that as

rewrite "www.subdomain.domain.com

to

"index.php?subdomain=subdomain"

If I am correct, then Birdman's advice might well fix it.

WBF

macdar

4:43 pm on Jul 6, 2005 (gmt 0)

10+ Year Member



Willybfriendly and Birdman, you're right!
That query after the domain name is not being passed.
I put some more stuff in my .htaccess file to see what's happening.

RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.domain\.com
RewriteRule ^([^.]+)\.html$ /index.php?subdomain=%2&v=$1 [L]

and it worked out.

however, that solution does not satisfy me, cause I have couple more parameters being passed along with the $v variable, like id's, etc..

I was not familiar with the QSA flag before, but that solution seems to be an ideal one in this situation, but unfortunately it didn't really cause anything - $v is still not being passed..

BTW, looks like now the post became more appropriate for the apache section.. Can you guys move it over?

Thanks.

jdMorgan

7:33 pm on Jul 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Upload the following code to your server. Then flush your browser cache (Temporary Internet Files) and test it. You must flush your cache before testing each and every change to your code.

RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.domain\.com
RewriteRule ^([^.]+)\.html$ /index.php?subdomain=%2&v=$1 [QSA,L]

If you request foo.domain.com/bar.html?floo=dust, then the request should be passed to /index.php?floo=dust&subdomain=foo&v=bar

Jim

macdar

9:09 pm on Jul 6, 2005 (gmt 0)

10+ Year Member



Thanks Jim for your response.

I did it what you suggested - and it worked out.

if ($v == "test") {
if($id == "1" && $catid == "2")
{
$sub = $_GET[subdomain];
echo $sub;
}

I type [somedomain.domain.com...]

At first I was a little bit concerned cause it's not as much SE friendly as I wanted it to be, but basically I added some more rewrite rules:

RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.domain\.com
RewriteRule ^url1_(.*).html /index.php?subdomain=%2&t=test1&id=$1 [QSA,L]

RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.domain\.com
RewriteRule ^url2_(.*)_(.*).html /cfadmin.php?subdomain=%2&v=test2&cat_id=$1&id=$2 [QSA,L]
etc..
And it seems to work fine.

The only concern I have is the repeating condition statement.. Is there any way to handle it in a more elegant way?

Once again, many thanks Jim.

jdMorgan

10:04 pm on Jul 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



At least you realized you *need* to repeat the condition... We get lots of problems reported here because it's not obvious that a RewriteCond only applies to the single RewriteRule that follows it.

If you only have a few rules, then repeating the RewriteCond is actually faster. Once you get beyond, say, three rules, then you can use a different construct that is less obvious and more complex.

Instead of


RewriteCond <condition1>
RewriteRule <rule1>
RewriteCond <condition1>
RewriteRule <rule2>
RewriteCond <condition1>
RewriteRule <rule3>

you could use

RewriteCond [b]!<c[/b]ondition1>
RewriteRule .* - [S=3]
RewriteRule <rule1>
RewriteRule <rule2>
RewriteRule <rule3>

Here, the first rule causes the next three rules to be skipped if the initial RewriteCond is NOT true. The downside is that you must maintain an accurate 'skip count' if you modify the ruleset later. However, if these rules are the last ones that need to be processed for any request, then you could replace the [S=3] with [L] and avoid that problem.

This technique is more useful as the number of 'shared' conditions increases, and as the number of rules increases. For fewer than three or four rules, it's just as well to use the longer, more straightforward approach.

Be aware that using negative logic like this introduces complications to combinatorial conditions; If you used to test for Cond1 AND Cond2, you will need to convert that to test for NOT(Cond1) OR NOT(Cond2). If this subject is new to you, a search on "DeMorgan's theorem" may be useful.

Jim

macdar

11:55 pm on Jul 6, 2005 (gmt 0)

10+ Year Member



Jim, thanks for the tutorial. And what's more important I'm showing even some signs of understanding!
I have more than ten rules so I'm gonna go with the more complicated solution.

I've followed your instructions quite thoroughly, however looks like I'm loosing the variable ($subdomain) passed through backreference(?)

here's my code:

RewriteCond %{HTTP_HOST}!^(www\.)?([^.]+)\.domain\.com
RewriteRule .* - [S=3]
RewriteRule ^list_(.*)_(.*).html /index.php?subdomain=%2&t=list&catid=$1&id=$2 [QSA]
RewriteRule ^edit_(.*).html /index.php?subdomain=%2&t=edit&id=$1 [QSA]
RewriteRule ^show_(.*).html /index.php?subdomain=%2&t=show&id=$1 [QSA,L]

I'm assuming this has got something to do with DeMorgan's theorem..otherwise you wouldn't mention that, right? However, honestly I don't see where could I apply these rules, cause I can't see any AND's and/or OR's..
Can anybody tell me what I'm missing in that code?

Thanks.

jdMorgan

2:12 am on Jul 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, you're losing the variable because it no longer exists (RewriteConds apply only to the rules that they precede). So now I'm going to have to introduce another complication: The user-defined variable.

# Extract subdomain if present
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.domain\.com
# If html filetype and subdomain is present, store it into user-defined variable and skip next rule
RewriteRule \.html$ - [E=subd:%2,S=1]
# No subdomain or non-html filetype, so exit
RewriteRule .* - [L]
# Rewrite html files using stored subdomain
RewriteRule ^list_(.*)_(.*).html /index.php?subdomain=%{ENV:subd}&t=list&catid=$1&id=$2 [QSA,L]
RewriteRule ^(edit¦show)_(.*).html /index.php?subdomain=%{ENV:subd}&t=$1&id=$2 [QSA,L]

Note that I included the pattern "\.html" in the first rule to make it as specific as possible (given what I know of your application). You may need to modify or remove that if you later add rewrites for non-html files. I also "compressed" your last two rules into one by adding the alternate pattern and a second back-reference. Be sure to change the broken pipe "¦" character to a solid pipe character from your keyboard before use -- posting on this board modifies that character.

Have fun! :)

Jim

macdar

4:49 pm on Jul 7, 2005 (gmt 0)

10+ Year Member



Jim your the man! This solution works like a charm!
I didn't even realize that all that rewriting stuff is so flexible, that you can define your own variables, etc..
I'm gonna dig a little bit in that. It's really cool!

Ok Jim, once again thanks for the great lesson, and hopefully I won't have to be a pain on your butt anymore:)

Have a good one,
Thanks.

jdMorgan

7:37 pm on Jul 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> I won't have to be a pain

No pain, no gain... :)

You're not a pain anyway; As a moderator, I don't *have* to answer anything... Just enforce the Terms of Service. I answer because I felt the same way about it that you do right now -- It is a very compact, very powerful tool, and I like it.

Jim