By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,405 Members | 1,691 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,405 IT Pros & Developers. It's quick & easy.

URL rewrite + htaccess

P: n/a
Hi there.
I have a domain, and i want every query string
redirected to index.php?
so domain.com/foo/bar would actually be domain.com/index.php?/foo/bar
Then i want to be able to say in index.php,
if QUERY_STRING == foo, do this etc.
(How) can i do this with htaccess?

thanks!

Jul 17 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
frizzle wrote:
Hi there.
I have a domain, and i want every query string
redirected to index.php?
so domain.com/foo/bar would actually be domain.com/index.php?/foo/bar
Then i want to be able to say in index.php,
if QUERY_STRING == foo, do this etc.
(How) can i do this with htaccess?

thanks!


RewriteEngine On
RewriteRule ^(.*)$ index.php?$1

If you want:
example.com/foo/bar/?id=10 to work:

RewriteRule ^(.*)$ index.php?$1 [QSA]

Then in index.php, your _GET array should (in theory, not tested) look like:

array(
'/foo/bar/' => '',
'id' => 10
)

$_SERVER['QUERY_STRING'] should be:
?/foo/bar/&id=10

Don't know how well

--
Justin Koivisto - ju****@koivi.com
http://koivi.com
Jul 17 '05 #2

P: n/a
frizzle wrote:
Hi there.
I have a domain, and i want every query string
redirected to index.php?
so domain.com/foo/bar would actually be domain.com/index.php?/foo/bar
Then i want to be able to say in index.php,
if QUERY_STRING == foo, do this etc.
(How) can i do this with htaccess?

thanks!


RewriteEngine On
RewriteRule ^(.*)$ index.php?$1

If you want:
example.com/foo/bar/?id=10 to work:

RewriteRule ^(.*)$ index.php?$1 [QSA]

Then in index.php, your _GET array should (in theory, not tested) look like:

array(
'/foo/bar/' => '',
'id' => 10
)

$_SERVER['QUERY_STRING'] should be:
?/foo/bar/&id=10

Don't know how well

--
Justin Koivisto - ju****@koivi.com
http://koivi.com
Jul 17 '05 #3

P: n/a
Wow, thanks!
Just what i needed!

Jul 17 '05 #4

P: n/a
Wow, thanks!
Just what i needed!

Jul 17 '05 #5

P: n/a
I'm sorry,
i cheered to early...

Without '[QSA]':
'domain.com' gives a 400 error.
'domain.com?foo' gives a 400 error.
'domain.com/' gives 'index.php'
'domain.com/foo' gives 'index.php'

With '[ QSA]'
'domain.com' gives a 400 error.
'domain.com?foo' gives a 400 error.
'domain.com/' gives 'index.php'
'domain.com/foo' gives 'index.php&foo' (should'nt that be
'index.php?foo' ?)
'domain.com/?foo' gives 'index.php&&foo' (should'nt that be
'index.php?&foo' ?)

Thanks in advance.

Jul 17 '05 #6

P: n/a
I'm sorry,
i cheered to early...

Without '[QSA]':
'domain.com' gives a 400 error.
'domain.com?foo' gives a 400 error.
'domain.com/' gives 'index.php'
'domain.com/foo' gives 'index.php'

With '[ QSA]'
'domain.com' gives a 400 error.
'domain.com?foo' gives a 400 error.
'domain.com/' gives 'index.php'
'domain.com/foo' gives 'index.php&foo' (should'nt that be
'index.php?foo' ?)
'domain.com/?foo' gives 'index.php&&foo' (should'nt that be
'index.php?&foo' ?)

Thanks in advance.

Jul 17 '05 #7

P: n/a
* bump *
Please.. ?

Jul 17 '05 #8

P: n/a
frizzle wrote:
I'm sorry,
i cheered to early...

Without '[QSA]':
'domain.com' gives a 400 error.
'domain.com?foo' gives a 400 error.
'domain.com/' gives 'index.php'
'domain.com/foo' gives 'index.php'

With '[ QSA]'
'domain.com' gives a 400 error.
'domain.com?foo' gives a 400 error.
'domain.com/' gives 'index.php'
'domain.com/foo' gives 'index.php&foo' (should'nt that be
'index.php?foo' ?)
'domain.com/?foo' gives 'index.php&&foo' (should'nt that be
'index.php?&foo' ?)

Thanks in advance.


Dunno... Try in alt.apache.configuration group, or try changing your method.

For instance, this is similar to what I use:

RewriteRule ^([^/]+)(/([^/]+))?(/([^/]+))?(/([^/]+))?/?$
x.php?v1=$1&v2=$3&v3=$5&v4=$7 [L,NS,QSA]

--
Justin Koivisto - ju****@koivi.com
http://koivi.com
Jul 17 '05 #9

P: n/a
I'm sure you're no idiot, but just a heads up incase you are unaware of
this. Be careful about any secure areas of your site to which you want
restricted access, for example PHP include folders or anything. I have
seen a few sites which use this "index.php gateway" approach, where
simply changing the string passed to the script allows access to
anything, sometimes even sending PHP source unparsed. This problem may
or may not affect you, depending on how you've done things.

Adam
Jul 17 '05 #10

P: n/a
First of all, thanks for your warning!

Though i haven't got a solution yet for my initial posted problem
i use sort of the following method:

$string = $_SERVER['QUERY_STRING'];

$allowed_strings = array('home', 'contact', 'etc');

if(!in_array($string, $allowed_strings))
{

$string = 'home';

};

I thought this was quite safe, but please do inform me if not!

Thanks!

Frizzle.

Jul 17 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.