SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Wizard bronze trophy cydewaze's Avatar
    Join Date
    Jan 2006
    Location
    Merry Land, USA
    Posts
    1,096
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Friendly URLs causing havok with pagination.

    I'm working on a photo gallery that uses "Friendly" URLs, and so far everything is working quite well.

    I have a Rewrite rule that makes this:
    /gallery/index.cfm?album=album&photo=44

    Look like this:
    /gallery/album/subalbum/44

    My problem now is that some albums are pretty huge, so I'd like to add pagination. I wrote something to append ?page=1 to the end of the URL string, but that seems to "break" my setup.

    Here are the rewrite rules I have now. Keep in mind that some albums have sub-albums (/album/sub-album/pic), and some just have pics (/album/pic)


    Code:
    RewriteRule gallery/(\w+)/$ /gallery/index.cfm?album=$1 [NC]
    RewriteRule gallery/(\w+)$ /gallery/index.cfm?album=$1 [NC]
    
    RewriteRule gallery/(\w+)/(\d+)$ /gallery/index.cfm?album=$1&photo=$2 [NC]
    RewriteRule gallery/(\w+)/(\d+)/$ /gallery/index.cfm?album=$1&photo=$2 [NC]
    
    RewriteRule gallery/(\w+)/(\w+)$ /gallery/index.cfm?album=$2 [NC]
    RewriteRule gallery/(\w+)/(\w+)/$ /gallery/index.cfm?album=$2 [NC]
    
    RewriteRule gallery/(\w+)/(\w+)/(\d+)$ /gallery/index.cfm?album=$2&photo=$3 [NC]
    RewriteRule gallery/(\w+)/(\w+)/(\d+)/$ /gallery/index.cfm?album=$2&photo=$3 [NC]
    This might look horrific to some of you, but I am a COMPLETE novice when it comes to Mod Rewrite.

    I'm just not sure how to add an argument for &page=$ to the URL without breaking what's there now.
    <cfset myblog = "http://cydewaze.org/">

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,653
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    cyd,

    Horrific? Close.

    Quote Originally Posted by cydewaze View Post
    I'm working on a photo gallery that uses "Friendly" URLs, and so far everything is working quite well.

    I have a Rewrite rule that makes this:
    /gallery/index.cfm?album=album&photo=44

    Look like this:
    /gallery/album/subalbum/44

    No you don't! The redirection is going the other way!

    My problem now is that some albums are pretty huge, so I'd like to add pagination. I wrote something to append ?page=1 to the end of the URL string, but that seems to "break" my setup.

    That's the function of the QSA flag - retain the original (page=whatever) query string.

    Here are the rewrite rules I have now. Keep in mind that some albums have sub-albums (/album/sub-album/pic), and some just have pics (/album/pic)


    Code:
    RewriteRule gallery/(\w+)/$ /gallery/index.cfm?album=$1 [NC]
    RewriteRule gallery/(\w+)$ /gallery/index.cfm?album=$1 [NC]
    
    RewriteRule gallery/(\w+)/(\d+)$ /gallery/index.cfm?album=$1&photo=$2 [NC]
    RewriteRule gallery/(\w+)/(\d+)/$ /gallery/index.cfm?album=$1&photo=$2 [NC]
    
    RewriteRule gallery/(\w+)/(\w+)$ /gallery/index.cfm?album=$2 [NC]
    RewriteRule gallery/(\w+)/(\w+)/$ /gallery/index.cfm?album=$2 [NC]
    
    RewriteRule gallery/(\w+)/(\w+)/(\d+)$ /gallery/index.cfm?album=$2&photo=$3 [NC]
    RewriteRule gallery/(\w+)/(\w+)/(\d+)/$ /gallery/index.cfm?album=$2&photo=$3 [NC]
    WARNING: You should NEVER follow a filename with a / as that changes the directory level of the request which will result in support files not being found (assuming you use relative links).

    Using the No Case flag in a RewriteRule will only cause you problems, too! NC is designed to be used on variables which are not case sensitive (URIs ARE case sensitive) like {HTTP_HOST}.


    Then, I'm not very enamored with your (correct) use of the word metacharacter (\w; I prefer [a-zA-Z]) but, if you understand regex, don't change it! Ditto the digit metacharacter (\d vs [0-9]).

    With those comments, try:
    Code:
    RewriteEngine on
    RewriteRule gallery/(\w+)$ gallery/index.cfm?album=$1 [QSA,L]
    
    RewriteRule gallery/(\w+)/(\d+)$ gallery/index.cfm?album=$1&photo=$2 [QSA,L]
    
    RewriteRule gallery/(\w+)/(\w+)$ gallery/index.cfm?album=$2 [QSA,L]
    
    RewriteRule gallery/(\w+)/(\w+)/(\d+)$ gallery/index.cfm?album=$2&photo=$3 [QSA,L]
    I've also removed the leading / in the redirection as that forces Apache to look to the server root before your DocumentRoot.

    This might look horrific to some of you, but I am a COMPLETE novice when it comes to Mod Rewrite.

    You might benefit from reading the mod_rewrite tutorial linked in my signature as it contains explanations and sample code. It's helped may members and should help you, too.

    I'm just not sure how to add an argument for &page=$ to the URL without breaking what's there now.
    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  3. #3
    SitePoint Wizard bronze trophy cydewaze's Avatar
    Join Date
    Jan 2006
    Location
    Merry Land, USA
    Posts
    1,096
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    DK, thanks for the candid assessment of my pseudocode. I think I'll read the tutorial linked in your sig, as it looks a bit more basic (notice-oriented) than some of the others I've tried to wrap my head around.
    <cfset myblog = "http://cydewaze.org/">

  4. #4
    SitePoint Wizard bronze trophy cydewaze's Avatar
    Join Date
    Jan 2006
    Location
    Merry Land, USA
    Posts
    1,096
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Just as a follow-up, I found that tutorial priceless. I had to make some changes to my site, most of which were changing links to remove the trailing slash. I'm used to adding this, because at work, out link checker reports any URLS that don't end in a slash (or a filename) as a broken link, so it was natural to do this for my website.

    Once I got rid of those and added the updated the flags, I was quickly able to get pagination working. So /gallery/bikes?page=2 works a charm. I've also cleaned up some of the other rewrite rules I had in place for other pages.

    Thanks again for the help.
    <cfset myblog = "http://cydewaze.org/">

  5. #5
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,653
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    cyde,

    Horray! (partier) I'm glad that the tutorial helped you, too!

    As an aside, the trailing slash SHOULD be used on directories but NEVER on files (because it changes directory level of the request and that affects the relative links). Apache handles directories (with or without) the trailing /'s but it requires Options MultiViews (which serves files in the path whether the file extension is present or not - an ugly feature to get around to I keep MultiViews turned off).

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


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
  •