[thelist] Re: Apache mod_rewrite and PHP SID

Tony Light TonyLight at blueyonder.co.uk
Wed Feb 5 09:46:01 CST 2003


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.





More information about the thelist mailing list