Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Faking the 'path' of a cookie - via .htaccess / javascript / or otherwise?

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.

like image 274
Jess Telford Avatar asked Jun 28 '11 02:06

Jess Telford


1 Answers

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]
like image 120
Francois Deschenes Avatar answered Oct 24 '22 01:10

Francois Deschenes