views:

33

answers:

1

Hello.

My last post was met by smarmy, unhelpful "answers" (comments), so i'll get right to it:

if I have an htaccess file like so:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)$ /index.php?page=$1&subject=$2
RewriteRule ^([^/]+)$ /index.php?page=$1 [L]

how can I allow for other url variable names and values to be handled... say for instance I want to add extra unexpected url vars to this scenario

/page/subject?urlvar1=value1&urlvar2=value2

and get the page I want without creating unexpected results?

Any real help is greatly appreciated. Thanks!

+2  A: 

you need to add [QSA] (query string append) flag to your rules.

As a general advice, it's better to have only one generic rule like

 RewriteRule .* index.php?path=$0

and parse the path at php level. This is much more flexible and transparent.

to elaborate, here's the complete setup

.htaccess

Options +FollowSymLinks

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php?path=$0 [QSA]

index.php

<pre>
<?php 
print_r($_GET);
?>
</pre>

when called like www.example.com/foo/bar/baz?quux=123&blah=456 this prints

Array
(
    [path] => foo/bar/baz
    [quux] => 123
    [blah] => 456
)

the only thing that's left is to parse $_GET['path'] with explode or regular expressions.

stereofrog
Lucas
+1 - I wasn't aware of the `[QSA]` flag. I just tried it, it works like a charm.
zombat
+1 for parsing with php instead of messing with htaccess. That's how I do it and it's way easier to get it working if you ever need to make it work on lighttpd or some other web server.
Syntax Error
@Lucas - try it with your existing rule set first before you change everything. The rules you had worked with `[QSA]` in the test I did. Stereofrog's suggestion to only have a single rule works only if you write some PHP code to handle the path string for every request. You might not want to jump into that just yet.
zombat
@stereofrog... so you're saying if i am processing a request like: /page/subject ... then that equals /index.php?path=/page/subject ? and then i parse the variables? that sounds like a dream! yes?
Lucas
qsa works!!! now, if people could quit being so snide :\ thanks, everyone, for all your help!!! :)
Lucas
added example code for the "path" thing
stereofrog