Click to See Complete Forum and Search --> : domain.com --> index.php?section=bob


cybercampbell
06-23-2006, 02:49 PM
Hi all

Hopfully a simple one...this is what I need to do...

redirect (or is it rewrite?)

domain.com
to
domain.com/index.php?section=bob

But keep the URL as the domain.com

any ideas?

Cheers
Chris

NogDog
06-23-2006, 03:03 PM
If on Apache, I believe a .htaccess rewrite rule could handle that. Or, since it's a PHP file, you could just check to see if you got there without a URL query for "section" and set it:

<?php
// do this before any other processing that deals with the $_GET array:
if(!isset($_GET['section']))
{
$_GET['section'] = 'bob';
}
// rest of script....

cybercampbell
06-23-2006, 03:10 PM
I need to do this with .htaccess as I have 3 URLs all pointing to the same htdocs directory.

this is what I need in more detail:

domain1.com ----> domain1.com/index.php?section=bob

domain2.com ----> domain2.com/index.php?section=sue

domain3.com ----> domain3.com/index.php?section=dan

And I have php doing the rest of the work.

and if posible I'd like to keep the URL as domain*.com

Cheers
Chris

cybercampbell
06-23-2006, 03:18 PM
I need to do this with .htaccess as I have 3 URLs all pointing to the same htdocs directory.

this is what I need in more detail:

domain1.com ----> domain1.com/index.php?section=bob

domain2.com ----> domain2.com/index.php?section=sue

domain3.com ----> domain3.com/index.php?section=dan

And I have php doing the rest of the work.

and if posible I'd like to keep the URL as domain*.com

Cheers
Chris

Scleppel
06-23-2006, 04:08 PM
It can still be dome ith PHP:
<?php

$domain = getenv('HTTP_HOST'); // grab the domain
if(!isset($_GET['section']) // if $_GET['section'] isn't set
&& preg_match('#^(www\.)?([^\.]+)\.com$#',$domain,$match))
// match the domain (without www. and tld)
{
switch($match[2])
{
case 'domain1':
$_GET['section'] = 'bob';
break;
case 'domain2':
$_GET['section'] = 'sue';
break;
case 'domain3':
$_GET['section'] = 'dan';
break;
}
}

Or with mod_rewrite:
Options +FollowSymLinks

RewriteEngine On

# domain1.com or www.domain1.com -> /index.php?section=bob
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com$ [NC]
RewriteCond %{QUERY_STRING} !section=
RewriteRule ^(index\.php)?$ /index.php?section=bob [L]

# domain2.com or www.domain2.com -> /index.php?section=sue
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com$ [NC]
RewriteCond %{QUERY_STRING} !section=
RewriteRule ^(index\.php)?$ /index.php?section=sue [L]

# domain3.com or www.domain3.com -> /index.php?section=dan
RewriteCond %{HTTP_HOST} ^(www\.)?domain3\.com$ [NC]
RewriteCond %{QUERY_STRING} !section=
RewriteRule ^(index\.php)?$ /index.php?section=dan [L]

Edit: updated mod_rewrite to add QUERY_STRING lines.

cybercampbell
06-23-2006, 04:37 PM
Perfect...cheers...I just forgot 1 thing:

domain.com

need to go to

domain.com/index.php

this seems to be broken now.

domain1 2 and 3 are now working perfect

any ideas?

Cheers

Scleppel
06-23-2006, 04:42 PM
Which one are you using?

cybercampbell
06-23-2006, 04:44 PM
sorry.....htaccess.

Cheers

Scleppel
06-23-2006, 05:03 PM
It shouldn't be causing a problem because it never matches your main domain, but you could try adding
# if www.domain.com or domain.com do nothing
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^(index\.php)?$ - [L]
after
RewriteEngine On
and see if that helps.

cybercampbell
06-23-2006, 05:18 PM
perfect...thanks heaps

I have one more thing I need to do...I'm hoping you can help.

I have this URL:

www.domain.com/user

and it goes to:

www.domain.com/index.php?site=user

and also this:

www.domain.com/user/dir

and it goes to:

www.domain.com/index.php?site=user&ext=dir

and all of this doesn't mess with the earlier rewrites I did:

i.e.
domain.com ----> domain.com/index.php
domain1.com ----> domain1.com/index.php?section=bob
domain2.com ----> domain2.com/index.php?section=sue
domain3.com ----> domain3.com/index.php?section=dan

and the term 'user' and 'dir' can be anything

e.g.
www.domain.com/tom/stats
www.domain.com/bob/images
www.domain.com/sue/file.html

ohh...and 'dir' can be either a directory or a file.

thanks agin

Cheers
Chris

Scleppel
06-23-2006, 05:35 PM
Just for domain.com? Add this to the end of your file:
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^([^/]+)/?$ /index.php?site=$1 [L]

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

For all domains? Add this to the end of your file:
RewriteRule ^([^/]+)/?$ /index.php?site=$1 [L]
RewriteRule ^([^/]+)/(.+)$ /index.php?site=$1&ext=$2 [L]

You'll probably need to add
RewriteCond %{SCRIPT_FILENAME} -f [OR]
RewriteCond %{SCRIPT_FILENAME} -d
RewriteRule .* - [L]
at the top of your file after
RewriteEngine On
aswell to stop if rewriting requests for real files and directories (including index.php).

cybercampbell
06-23-2006, 05:41 PM
F*%ken perfect

I love .htaccess

Cheers mate...much appreciated.

Cybercampbell

cybercampbell
06-24-2006, 03:56 AM
Hi mate....just found a snag.

if I do this: (this is just for domain.com)

domain.com/user
I get:
index.php?site=user

domain.com/user/dir
I get:
index.php?site=user&ext=dir

domain.com/user/dir1/dir2/dir3
I get:
index.php?site=user&ext=dir1/dir2/dir3

domain.com/user/file.html
I get:
index.php?site=user&ext=file.html

domain.com/user/dir1/dir2/file.php
I get:
index.php?site=user&ext=dir1/dir2/file.php

this is all perfect.....but this doesn't:

domain.com/user/file.php?var1=one&var2=two
I get:
index.php?site=user&ext=file.php
but need this:
index.php?site=user&ext=file.php?var1=one&var2=two

domain.com/user/dir1/dir2/file.php?var1=one&var2=two
I get:
index.php?site=user&ext=dir1/dir2/file.php
but need this:
index.php?site=user&ext=dir1/dir2/file.php?var1=one&var2=two

any ideas?

Scleppel
06-24-2006, 07:17 AM
Replace the [L] flag on these two lines
RewriteRule ^([^/]+)/?$ /index.php?site=$1 [L]
# and
RewriteRule ^([^/]+)/(.+)$ /index.php?site=$1&ext=$2 [L]
with [QSA,L]. The QSA (Query String Append) flag will add the query string onto the end of the new URL.

cybercampbell
06-24-2006, 07:36 AM
aaaahhhh!!! didn't work

I have this:


RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^([^/]+)/?$ /usertest.php5?user=$1 [QSA,L]

RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^([^/]+)/(.+)$ /usertest.php5?user=$1&ext=$2 [QSA,L]


have I got something wrong?

Scleppel
06-24-2006, 07:45 AM
What's wrong with it? Have you dumped the $_GET array?

From an input of "/user/dir1/dir2/file.php?var1=one&var2=two" you should get:
GET:
Array
(
[site] => user
[ext] => dir1/dir2/file.php
[var1] => one
[var2] => two
)

You can't have (as far as i know)
GET:
Array
(
[site] => user
[ext] => dir1/dir2/file.php?var1=one&var2=two
)
because mod_rewrite won't encode the "&"s, even if you did something like this
RewriteRule ^([^/]+)/(.+)$ /usertest.php5?user=$1&ext=$2?%{QUERY_STRING} [L]

cybercampbell
06-24-2006, 07:51 AM
sorry mate....you are absolutly correct!

I need to sort my PHP now.

Cheers for all your help.

cybercampbell
06-24-2006, 12:41 PM
OK...now i need a little help with my PHP.

I now have this:

index.php?site=user&ext=dir1/dir2/file.php?var1=one&var2=two

and I can get these variables:

$_GET['user']
$_GET['ext']

but how do I get the var1 and var2...keeping in mind that these could be any variables

i.e.

index.php?site=user&ext=dir1/dir2/file.php?bark=brown&tree=green
or
index.php?site=user&ext=dir1/dir2/file.php?time=630&day=tuesday

is there a way?

Cheers
Chris

Scleppel
06-24-2006, 12:49 PM
If you are using the code i gave you (with the QSA) the URL would be "index.php?site=user&ext=dir1/dir2/file.php&var1=one&var2=two".

You would access them exactly how you are accessing "site" and "ext", through the _GET array:
echo $_GET['site']; // user
echo $_GET['ext']; // dir1/dir2/file.php
echo $_GET['var1']; // one
echo $_GET['var2']; // two

cybercampbell
06-24-2006, 12:57 PM
Sorry...missed the & thing...that sorted.

The thing is...is not always var1 and var2

I need to be able to pick any variable names

sometimes instead of 'var1' & 'var2 'it could be 'bark' & 'tree' or 'time' & 'day'

can the php script pick them up...whatever they are and then give them new varaible names?

is this posible?

Scleppel
06-24-2006, 01:21 PM
All variables will be passed through by mod_rewrite if the QSA flag is given. But you have to know the names of the variables to access them unless you are going to loop through them (foreach). This is just like normally accessing _GET variables in PHP.

cybercampbell
06-24-2006, 01:23 PM
ok...i'll check out foreach

any quick code for that?

maybe i'll google it

Cheers

cybercampbell
07-04-2006, 09:18 AM
Hi Scleppel

I have to re-look at this htaccess and I'm hopping you can help me

this is relating to:

RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^([^/]+)/?$ /usertest.php5?site=$1 [QSA,L]

RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^([^/]+)/(.+)$ /usertest.php5?site=$1&ext=$2 [QSA,L]

OK...(this does my head in) I now need it to go from:

domain.com/user or www.domain.com/user
to
usertest.php5?site=user
also with the trailing slash
domain.com/user/ or www.domain.com/user/
to
usertest.php5?site=user
and then this:
domain.com/user/src?some=string&bla=blo or www.domain.com/user/src?some=string&bla=blo
to
usertest.php5?site=user&some=string&bla=blo

Scleppel
07-04-2006, 10:21 AM
So you want to ignore the "src" bit?

Add this:
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^([^/]+)/src/?$ /usertest.php5?site=$1 [QSA,L]
before this:
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^([^/]+)/(.+)$ /usertest.php5?site=$1&ext=$2 [QSA,L]

cybercampbell
07-04-2006, 10:28 AM
Cheers

what about the trailing slash problem...any ideas?

Thanks Chris

Scleppel
07-04-2006, 10:37 AM
/? means the trailing slash is optional, there shouldn't be any problems. Which rule are you having probelms with?

cybercampbell
07-04-2006, 10:39 AM
Just checked again and it's fine...sorry...late night.

Cheers again
Chris

cybercampbell
07-04-2006, 11:26 AM
OK this is the problem…

If I have the trailing slash: www.domain.com/user/

None of the images work in the page….works fine without the slash.

With the slash the image url becomes: www.domain.com/user/images/image.jpg ....when it should be www.domain.com/images/image.jpg

Any ideas?

Cheers
Chris

Scleppel
07-04-2006, 11:43 AM
You have to make all the paths to the images/css absolute, eg. "/images/image.jpg" instead of "images/image.jpg".

cybercampbell
07-08-2006, 09:59 AM
Cheers...all good.

Just one last question:

is it possible to use htaccess to hide all the query strings in the address bar so it always say: http://www.mydomain.com but still passes all the variable to the page.

Cheers
Chris

Scleppel
07-08-2006, 10:24 AM
I'm not positive i understand, but if you want to do what i think you want to do, then no. Whatever you put in the address bar stays in the address bar (unless you redirect, but if you redirect all variables will be lost). The variables have to be stored somewhere, either POSTed, in the query string, or in cookies. You need something in the URI or how would the users bookmark the different pages, etc.

cybercampbell
07-08-2006, 10:27 AM
I thought that was the case...now I know it is.

Cheers again

cybercampbell
08-01-2006, 06:40 AM
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^([^/]+)/?$ /usertest.php5?site=$1 [QSA,L]


This is all great...I just need to add support for subdomains now

eg:

cybercampbell.mydomain.com

gives me

mydomain.com/usertest.php5?site=cybercampbell

I tried meyself but no luck.

any ideas?

Cheers

Scleppel
08-01-2006, 04:09 PM
Do you have DNS for the subdomains (probably Wildcard)?
Do all the subdomains point to your main document root?

Please post your full current mod_rewrite code.

cybercampbell
08-01-2006, 04:16 PM
yes they do..this is the sort of thing I've used in the past:

RewriteCond %{HTTP_HOST} sub.domain.com
RewriteCond %{REQUEST_URI} !sub/
RewriteRule ^(.*)$ sub/$1 [L]

Scleppel
08-01-2006, 05:16 PM
On it's own, this should work:
Options +FollowSymLinks

RewriteEngine On

# cybercampbell.mydomain.com/ --> /usertest.php5?site=cybercampbell
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.com$ [NC]
RewriteRule ^$ /usertest.php5?site=%1 [L]
But you didn't show your full current mod_rewrite code so it might not work with what you've currently got.

You also haven't said what you want to happen when someone puts cybercampbell.mydomain.com/somehting-here in.

cybercampbell
08-01-2006, 06:38 PM
Sorry mate...this is what I have:

directoryindex index.php index.php5 index.html index.htm

Options +FollowSymLinks

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} -f [OR]
RewriteCond %{SCRIPT_FILENAME} -d
RewriteRule .* - [L]

RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^([^/]+)/?$ /usertest.php5?site=$1 [QSA,L]

RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^([^/]+)/src/?$ /usertest.php5?site=$1 [QSA,L]


so this all works great :)

but I just need to add support for subdomains...eg:

eg:

cybercampbell.mydomain.com

gives me

mydomain.com/usertest.php5?site=cybercampbell

as for the stuff after that..well...I guest it needs to be passed like this:

cybercampbell.mydomain.com/src?var1=one&var2=two

so aslong as that gets passed then I can deal with it like with the first example you gave me.

I hope this is what you asked.

Cheers

cybercampbell
08-01-2006, 06:55 PM
Let me just add one more thing...this is on a different project than before and I only need to use subdomains. but I will need to have stuff after the domain eg:

cybercampbell.mydomain.com/src?var1=one&var2=two

Cheers

Scleppel
08-01-2006, 07:09 PM
but I just need to add support for subdomains...eg:
You trying to add support for subdomains to the above code, right? Or did i confuse the situation asking for the full code?

This should work together, or you can remove what you don't want (the last two blocks):
DirectoryIndex index.php index.php5 index.html index.htm

Options +FollowSymLinks

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} -f [OR]
RewriteCond %{SCRIPT_FILENAME} -d
RewriteRule .* - [L]

RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.com$ [NC]
RewriteRule ^(.*)$ /usertest.php5?site=%1&uri=$1 [QSA,L]

RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^([^/]+)/?$ /usertest.php5?site=$1 [QSA,L]

RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^([^/]+)/src/?$ /usertest.php5?site=$1 [QSA,L]

cybercampbell
08-08-2006, 04:38 PM
Sorry I didn't get back sooner...been a bit under the weather :(

Anyway...I have a few problems with that code.

This is what I have at present:

directoryindex index.php index.php5 index.html index.htm

Options +FollowSymLinks

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} -f [OR]
RewriteCond %{SCRIPT_FILENAME} -d
RewriteRule .* - [L]

RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.com$ [NC]
RewriteRule ^(.*)$ /usertest.php5?htaccess_id=%1&uri=$1 [QSA,L]


OK...this works if the URL is:
http://123.mydomain.com/src?bla=bla&bli=bli

gives me:
http://123.mydomain.com/usertest.php5?htaccess_id=123&uri=bla=bla&bli=bli

but not if there are not sting queries or ext directories...eg:
http://123.mydomain.com

gives me:
http://mydomain.com/

and grabs the index.php5 file

Any ideas?

I dont need anything but the above options:

Cheers

Scleppel
08-08-2006, 04:54 PM
Replace
RewriteRule .* - [L]
with
RewriteRule !^$ - [L]

cybercampbell
08-12-2006, 07:42 PM
thanks...all good :)

cybercampbell
08-15-2006, 02:21 PM
OK...this is where I'm at and everythings is perfect!!!! yay :)

But know I needing to complicate things...Below is what I have (on two different sites) and what I need to happen now (on both sites)

The first site (with extention directory):

Options +FollowSymLinks

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} -f [OR]
RewriteCond %{SCRIPT_FILENAME} -d
RewriteRule .* - [L]

RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^([^/]+)/?$ /site.php5?user=$1 [QSA,L]

RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^([^/]+)/src/?$ /site.php5?user=$1 [QSA,L]

I need to add this:

http://www.mydomain.com/mydir/stats
http://www.mydomain.com/mydir/stats/
gives me:
/stats/stats.php5?stats_username=mydir

there will be images used in this directory which will need to work:
http://www.mydomain.com/mydir/stats/images/image.jpg

The second site (with subdirectory):

Options +FollowSymLinks

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} -f [OR]
RewriteCond %{SCRIPT_FILENAME} -d
RewriteRule !^$ - [L]

RewriteCond %{HTTP_HOST} !^www\.domainname\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.domainname\.com$ [NC]
RewriteRule ^(.*)$ /site.php5?id=%1&uri=$1 [QSA,L]

I need to add this:

http://mysub.domainname.com/stats
http://mysub.domainname.com/stats/
gives me:
/stats/stats.php5?stats_username=mysub

there will be images used in this directory which will need to work:
http://mysub.domainname.com/stats/images/image.jpg

I hope this all makes sence.

This will be the last thing on this one...promise :D

Cheers
Cybercampbell

cybercampbell
08-19-2006, 06:18 PM
I'm getting desperate here....any takers....please!!!!

Scleppel
08-19-2006, 06:46 PM
If you were that desperate you'd learn about mod_rewrite.

The first:
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} -f [OR]
RewriteCond %{SCRIPT_FILENAME} -d
RewriteRule .* - [L]

RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^([^/]+)/?$ /site.php5?user=$1 [QSA,L]

RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^([^/]+)/src/?$ /site.php5?user=$1 [QSA,L]

RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^([^/]+)/stats/?$ /stats/stats.php5?stats_username=$1 [QSA,L]
(It's just the same as the "src" rule with the "src" replaced with "stats" and the path changed.)

The second:
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} -f [OR]
RewriteCond %{SCRIPT_FILENAME} -d
RewriteRule !^$ - [L]

RewriteCond %{HTTP_HOST} !^www\.domainname\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.domainname\.com$ [NC]
RewriteRule ^stats/?$ /stats/stats.php5?stats_username=%1 [QSA,L]

RewriteCond %{HTTP_HOST} !^www\.domainname\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.domainname\.com$ [NC]
RewriteRule ^(.*)$ /site.php5?id=%1&uri=$1 [QSA,L]

cybercampbell
08-19-2006, 06:51 PM
Ohh...I try to do these things myself....I don't think I have to brain for it...Maybe I should buy a book.....any suggestions? infact I'd love to learn regular expression....any suggested sites or books for that too.

Thanks again for your help :)

Cheers
Cybercampbell

Scleppel
08-20-2006, 05:57 AM
As far as i now there arn't any mod_rewrite books. But this is a good site to learn regular expressions: http://www.regular-expressions.info/.