SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot
    Join Date
    May 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mod_rewrite and js cookie

    Hi all,

    I have an issue with mod rewrite. I am using javascript(mootools) for creating a cookie. This cookie stores text size preference chosen by user.

    Script works fine on all page except those redirected by mod_rewrite. I am a newbie for this.

    Here's sample of what I have written in .htacess file.

    Code:
    RewriteEngine On
    Options +Followsymlinks
    RewriteBase /localsite
    
    RewriteRule my-page/ page.1.0.php [NC]
    RewriteRule my-page-second/ page.1.1.php [NC]
    If I access page by its actual name, it works. Please help.

    Thanks a lot!

  2. #2
    SitePoint Addict skunkbad's Avatar
    Join Date
    Apr 2008
    Location
    Temecula, CA
    Posts
    278
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem may be with mod_rewrite, but also your link to the javascript. You may need to ensure an absolute link.

    Typically, in a RewriteRule, you will have what is called a back-reference, which is a group within parenthesis on the left side, and then a number with a dollar sign on the right.

    You might try something like this:

    RewriteEngine On
    Options +Followsymlinks
    RewriteBase /

    RewriteRule ^my-page(.*)\.php page$1.php [NC,L]

    This would rewrite a request for yoursite.com/mypage-1.php to yoursite.com/page-1.php

    you can even change the file extension, and nobody would know it was php.

    RewriteRule ^my-page(.*)\.php page$1.html [NC,L]

    I use a google sitemap on a site, and it is generated by php:

    RewriteRule sitemap.xml sitemap.php?type=xml [NC,L]

    but for the yahoo urllist.txt version:

    RewriteRule urllist.txt sitemap.php?type=txt [NC,L]
    Last edited by skunkbad; May 16, 2009 at 08:31. Reason: additional info

  3. #3
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Most likely the cookie is restricted to a specific path and by rewriting the request URI you’re messing it up. Use some kind of cookie viewer to look at its details — that way you will be able to see what the problem is.

  4. #4
    SitePoint Zealot adam.jimenez's Avatar
    Join Date
    May 2009
    Location
    Ware, UK
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yep - it does sound like the cookie path.

    try setting the cookie path to '/' so that it's accessible from the whole domain

  5. #5
    SitePoint Zealot
    Join Date
    May 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys, I'll try your suggestions.

    I'm using relative url(with html base tag).

    How should I set the cookie path? Do you mean to set the domain path?

    Thanks again!

  6. #6
    SitePoint Zealot adam.jimenez's Avatar
    Join Date
    May 2009
    Location
    Ware, UK
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the path of the cookie

    see the mootools docs - i'm not allowed to link to it!

    google: mootools cookie

  7. #7
    SitePoint Zealot
    Join Date
    May 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by skunkbad View Post
    The problem may be with mod_rewrite, but also your link to the javascript. You may need to ensure an absolute link.

    Typically, in a RewriteRule, you will have what is called a back-reference, which is a group within parenthesis on the left side, and then a number with a dollar sign on the right.

    You might try something like this:

    RewriteEngine On
    Options +Followsymlinks
    RewriteBase /

    RewriteRule ^my-page(.*)\.php page$1.php [NC,L]

    This would rewrite a request for yoursite.com/mypage-1.php to yoursite.com/page-1.php

    you can even change the file extension, and nobody would know it was php.

    RewriteRule ^my-page(.*)\.php page$1.html [NC,L]

    I use a google sitemap on a site, and it is generated by php:

    RewriteRule sitemap.xml sitemap.php?type=xml [NC,L]

    but for the yahoo urllist.txt version:

    RewriteRule urllist.txt sitemap.php?type=txt [NC,L]
    I tested you code, it's working.

    I want to use directory path like -

    RewriteRule ^my-dir/my-page(.*)\.php page$1.html [NC,L]

    How should I write the syntax?

    Can I hide extension file extension? Just want to show / instead of .php or .html.

    Thanks a lot!!!!!!!!

  8. #8
    SitePoint Zealot
    Join Date
    May 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    skunkbad is right. The problem is with mod_rewrite syntax, not with mootool cookie.

    skunkbad, please help me adding a directory to url.

    Thanks all for helping me!

  9. #9
    SitePoint Addict skunkbad's Avatar
    Join Date
    Apr 2008
    Location
    Temecula, CA
    Posts
    278
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want to leave off the file extension, you modify the rewriterule a little:

    RewriteRule ^my-page(.*)\.php page$1 [NC,L]

    if the rewriterule needs to be directory sensitive, then include another backreferece:

    RewriteRule ^(.*)my-page(.*)\.php $1page$2 [NC,L]


    I haven't tested this last one, but it should work. If you want to really learn what you are doing, look up some tutorials on mod_rewrite, and learn regular expressions (aka regex). Also, I bought a nice program called RegexBuddy, and it really helps to learn regex. As far as I know, it is only for Windows, but Mac or Linux might have something similar available.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •