URL rewrite + htaccess

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/...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-10-2005
frizzle
 
Posts: n/a
Default URL rewrite + htaccess

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!

Reply With Quote
  #2 (permalink)  
Old 05-10-2005
Justin Koivisto
 
Posts: n/a
Default Re: URL rewrite + htaccess

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
Reply With Quote
  #3 (permalink)  
Old 05-10-2005
frizzle
 
Posts: n/a
Default Re: URL rewrite + htaccess

Wow, thanks!
Just what i needed!

Reply With Quote
  #4 (permalink)  
Old 05-10-2005
frizzle
 
Posts: n/a
Default Re: URL rewrite + htaccess

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.

Reply With Quote
  #5 (permalink)  
Old 05-11-2005
frizzle
 
Posts: n/a
Default Re: URL rewrite + htaccess

* bump *
Please.. ?

Reply With Quote
  #6 (permalink)  
Old 05-11-2005
Justin Koivisto
 
Posts: n/a
Default Re: URL rewrite + htaccess

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
Reply With Quote
  #7 (permalink)  
Old 05-14-2005
Adam King
 
Posts: n/a
Default Re: URL rewrite + htaccess

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
Reply With Quote
  #8 (permalink)  
Old 05-14-2005
frizzle
 
Posts: n/a
Default Re: URL rewrite + htaccess

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.

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 11:07 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0