8

Background

I have php/js software (Piwik), which sets a cookie to track visits to the site.

Our site (ie; not Piwik) is setup so that all URLs (except for resources) are written back to /public/index.php.

This way, our users each get a unique URL, such as;

http://www.example.com/user1

http://www.example.com/user2

... etc

For me to track each of these user URLs in Piwik, it has been suggested that I need to set the path in the cookie to one that Apache maps to an actual directory.

Since we don't have actual directories for each of our users, we cannot do this.

Finally, we move on to using RewriteBase within the .htaccess to tell Apache that we consider the user's URL to be its own directory.

This falls short, however, as there does not appear to be a way to use RewriteBase without hardcoding the 'base'.

The question

Can I do something like this in my .htaccess? Francois Deschenes's answer says I cannot do this.

RewriteCond ^([^/]*)(/.*)?
RewriteBase %1

What other alternatives do I have for ensuring the 'path' of the cookie is set to be the user's URL rather than just '/'?

What I have currently

The .htaccess in / contains;

RewriteRule ^(.*)$ /public/$1 [L]

Then the .htaccess in /public contains;

RewriteRule ^index\.php5/(.*)$ -                   [L]
RewriteRule ^index\.php5?(.*)$ -                   [L]
RewriteRule ^index\.php5$      -                   [L]

RewriteRule ^(.*)$             /public/index.php5  [L]

Note that both of these have other rules at the beginning for paths that have moved, etc.

Thanks for any and all help!

What I have tried

Calling .setCookiePath() on the JS Piwik object, as suggested in the Piwik documentation. For example, for the URL http://www.example.com/user1 , calling .setCookiePath('/user1') does not infact set the cookie's path.

Adding a trailing slash to the URL, then calling .setCookiePath(). For example, the URL http://www.example.com/user1/ then calling .setCookiePath('/user1') does not set the cookie's path.

Related questions

With mod_rewrite can I specify a RewriteBase within a RewriteCond? - Unfortunately the answer doesn't indicate if I can use the current path as the base.

Community
  • 1
  • 1
Jess Telford
  • 12,880
  • 8
  • 42
  • 51
  • Curious as to the close vote? I'd love feedback on how this could be a more on-topic question :) – Jess Telford Jun 28 '11 at 23:38
  • I think you need to set cookie domain too, most browsers require domain and do not set cookies without it – venimus Jun 30 '11 at 14:01
  • The cookie is correctly being set, but the 'path' of the cookie is incorrect. Piwik sets the domain within its JS code, and calling `.setCookieDomani()` does not change anything. – Jess Telford Jul 01 '11 at 01:01

1 Answers1

6

You can't do that, with mod_rewrite. Apache and mod_rewrite have nothing to do with cookies (at least as far as Piwik is concerned). This isn't an Apache-related issue but how Piwik sets the cookie. If you're using the Piwik JavaScript tracker, have a look at this specific section of the documentation, more specifically the "If you track one domain sub directories or pages in different Piwik websites" section.

You'll essentially want to use the setCookiePath function and wherever you set the JavaScript, use PHP to fill in the path:

Javascript:

tracker.setCookiePath('/user1');

To add a trailing slash in .htaccess

Add this on top of your other rules:

RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://example.org/$1/ [L,R=301]
Francois Deschenes
  • 24,816
  • 4
  • 64
  • 61
  • Thanks for the info - I have linked to [this](http://forum.piwik.org/read.php?2,78381,page=1#msg-78690) thread in the question which suggests that it is necessary to have an actual directory (or to fake it with Apache) before a cookie can be set on that path. I have tried setCookiePath() with no success (see the thread for more details). – Jess Telford Jun 28 '11 at 04:56
  • @Jess Telford - I've already looked at the thread. It's suggesting that you use Apache to "fake" a folder which you've already done in your `.htaccess` files. You might need to add a trailing slash to your URLs for it to work (i.e. http://example.org/user1/). You could change your `.htaccess` to append the trailing slash if it's missing if it's unrealistic to change how the links are generated (or if users are accessing them directly. – Francois Deschenes Jun 28 '11 at 05:00
  • Unfortunately a trailing slash makes no difference :( – Jess Telford Jun 29 '11 at 01:29