This is a discussion on URL rewrite + htaccess within the PHP Language forums, part of the PHP Programming Forums category; Hi there. I have a domain, and i want every query string redirected to index.php? so domain.com/foo/...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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! |
|
|||
|
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 - justin@koivi.com http://koivi.com |
|
|||
|
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. |
|
|||
|
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 - justin@koivi.com http://koivi.com |
|
|||
|
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 |
|
|||
|
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. |