[thelist] Re: Apache mod_rewrite and PHP SID

Rob Whitener rwhitener at DesignOptions.com
Wed Feb 5 09:59:01 CST 2003


Couldn't you modify your regexp to account for such a scenario?  if you are
always going to have the n after the backslash couldn't you look for a
pattern like ?var=val after the n?  Group the Query string in parenthesis
and use a backreference to recover it.  Here is an example of something I do
in an .htaccess file:

RewriteEngine on
Options +FollowSymlinks
RewriteBase /newsite
RewriteRule prodbulletin_2/([a-zA-Z0-9]+) prodbulletin_2.php?type=$1
RewriteRule casestudy/([a-zA-Z0-9]+)/([a-zA-Z0-9]+) casestudy.php?$1=$2

these allow me to handle urls of the form
prodbulletin_2/value
and
casestudy/var/value
and redirect to their respective urls (I do this from an <a href> link, not
a form).
But I think if you write the regexp to look for the name value pair and
group name and value separately, you will be able to use $1 and $2 to get
the values.

Hope this helps,

Rob

-----Original Message-----
From: Tony Light [mailto:TonyLight at blueyonder.co.uk]
Sent: Wednesday, February 05, 2003 10:46 AM
To: thelist at lists.evolt.org
Subject: [thelist] Re: Apache mod_rewrite and PHP SID


Original problem:
<><><><><><><><><><><><><><><><><><><><><><>
> I'm experimenting with Apache's mod_rewrite (in order to make my sites
more search engine friendly).
>
> First experiment used the following rule...
> RewriteRule ^/([0-9])/?$                /play/modrewrite/echo.php?id=$1
>
> This takes urls of the form www.mydomain.com/n (where n is a single digit
numeric - and with or without trailing slash) and calls
/play/modrewrite/echo.php with ?id=n appended.
>
> All hunky dory.
>
> Now the question....
> How can I deal with a query string appended after the url?
>
> e.g. www.mydomain.com/n?extrainfo=something
>
> Such a url will still be intercepted by the RewriteRule above, and
'redirected' to /play/modrewrite/echo.php.
> However - the query string (even though it appears in the browser address
bar) is ignored.
<><><><><><><><><><><><><><><><><><><><><><>

Liam Delahunty promptly replied...

<><><><><><><><><><><><><><><><><><><><><><>
> While I can't answer you question about mod_rewrite as I've never played
> with it, the following snippet will test the cookie (ShoppingCart) and
> append the session only if it's needed. Therefore no extra "?" in the URI.
> Obviously watch for wrap...
> print ("<a href=\"../onlinesales/shop.php".(!$ShoppingCart?
> "?session=$session":"")."\">Shop</a><br>\n");
>
> Obviously, when there's other fields I append it to the end of the URL
with
> an ampersand.
> <a href=\"shop.php?category_id=$category_id".(!$ShoppingCart?
> "&session=$session":"")."\">$category</a>
<><><><><><><><><><><><><><><><><><><><><><>

Thanks for the prompt reply Liam (sorry not to get back quicker, but I'm on
the digest).

My problem is not with generating the session id in the query string part of
the url (for which I'm sure your suggestion will work admirably).

The problem is that mod_rewrite seems to cause query strings to drop into a
large black hole.
I can't pick up the query string with a RewriteRule in the Apache config
file, and it does not get passed on.

Typically, mod_rewrite might be used so that a url like
www.mydomain.com/products/123 (which is what the user and the search engines
would see) is actually handled by a script which behaves as if
www.mydomain.com/scripts/productcatalog.php?id=123 was the url.

Note: no query string in the actual url used - but after the rewrite there
is a query string.

If we use a url like: www.mydomain.com/products/123?session=12345, then the
user will still see that in their browser (fwiw) but the rewritten url will
still be www.mydomain.com/scripts/productcatalog.php?id=123 (which can be
determined by getting PHP to print out the script name and the query
string).  i.e. no sign of the $session variable.

It might be possible to treat the session id in the same way as the product
id in the above example - at least mod_rewrite would then pass it on.
e.g. www.mydomain.com/products/123/12345 would become
www.mydomain.com/scripts/productcatalog.php?id=123&session=12345
... but that totally defeats the objective of having search-engine friendly
urls.

If anybody is still reading this and has not lost the will to live, can I
remind you that the only requirement I have for using a query string on the
end of a url that is to be subjected to mod_rewrite is:
To add the PHP variable SID (so that the session id is propagated when
Cookies are disabled).

All suggestions welcomed.

Regards,
    Tony.


--
* * Please support the community that supports you.  * *
http://evolt.org/help_support_evolt/

For unsubscribe and other options, including the Tip Harvester
and archives of thelist go to: http://lists.evolt.org
Workers of the Web, evolt !



More information about the thelist mailing list