SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Nov 2002
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mod_rewrite Problem

    Maybe Pippo can explain what's up with this.

    When I do this mod_rewrite:

    Code:
    RewriteEngine On
    RewriteBase /
    RewriteRule ^albums/(.*)/?$ /visuals.php?album=$1 [L]
    It won't work if there's a slash at the end of the URL (I get a MySQL error), but it works without that final slash.

    When I do this mod_rewrite:

    Code:
    RewriteEngine On
    RewriteBase /
    RewriteRule ^albums/(.*)/$ /visuals.php?album=$1 [L]

    It works with a slash at the end, but won't work if there's no slash (a 404).

    What's the way to go so that it works with or without a slash at the end?

  2. #2
    SitePoint Zealot
    Join Date
    Nov 2002
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Pippo?
    Pippo?
    Bueller?

  3. #3
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i'm guessing it's because .* is greedy in the first one. pippo, can you do non-greedy .*? expressions in mod_rewrite?

    anyway, it looks like in this case, you can use this:

    Code:
    RewriteRule ^albums/([^/]+)/?$ /visuals.php?album=$1 [L]
    i used + instead of * since i assume you want it to match something. maybe that's wrong though...
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  4. #4
    FreeBSD The Power to Serve silver trophy pippo's Avatar
    Join Date
    Jul 2001
    Location
    Italy
    Posts
    4,514
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ahem,
    sorry guys I was a bit busy...I had some troubles configuring my home network and I didn't have time to look here.
    sorry again, I'm back now... :-)

    DrLarry suggestion is what I'd suggested,
    so it's perfectly right!

    Some notes:

    ^albums/(.*)/?$
    will accept
    albums/ <<-- $1 empty
    albums// <<-- $1 empty
    albums/name <<-- $1 is name
    albums/name/ <<-- $1 is name/
    albums/long/path/to/ <<-- $1 is long/path/to/
    which is wrong

    ^albums/(.*)/$
    will accept
    albums// <<-- $1 is empty
    albums/name/ <<-- $1 is name
    albums/long/path/to/ <<-- $1 is long/path/to
    which is wrong

    Larry's suggestion,
    will accept
    albums/name <<-- $1 is name
    albums/name/ <<-- $1 is name
    it won't accept
    albums/
    albums/long/name
    albums//
    which is very fine!


    :-) pippo
    Mr Andrea
    Former Hosting Team Advisor
    Former Advisor of '03

  5. #5
    SitePoint Zealot
    Join Date
    Nov 2002
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pippo
    ahem,
    sorry guys I was a bit busy...I had some troubles configuring my home network and I didn't have time to look here.
    sorry again, I'm back now... :-)

    DrLarry suggestion is what I'd suggested,
    so it's perfectly right!

    Some notes:

    ^albums/(.*)/?$
    will accept
    albums/ <<-- $1 empty
    albums// <<-- $1 empty
    albums/name <<-- $1 is name
    albums/name/ <<-- $1 is name/
    albums/long/path/to/ <<-- $1 is long/path/to/
    which is wrong

    ^albums/(.*)/$
    will accept
    albums// <<-- $1 is empty
    albums/name/ <<-- $1 is name
    albums/long/path/to/ <<-- $1 is long/path/to
    which is wrong

    Larry's suggestion,
    will accept
    albums/name <<-- $1 is name
    albums/name/ <<-- $1 is name
    it won't accept
    albums/
    albums/long/name
    albums//
    which is very fine!


    :-) pippo
    New question...

    I tried that way, and it works with or without the slash.

    However, I have my PHP script configured so that if there is no querystring, it displays a synopsis of all the albums.

    If I just go:

    www.thebigtymer.com/albums/

    it should display the default, but instead, I get a 404.

    But if I go:

    www.thebigtymer.com/albums/1/
    www.thebigtymer.com/albums/1
    www.thebigtymer.com/albums/2/
    www.thebigtymer.com/albums/2

    it works fine.

    What am I missing here?

  6. #6
    FreeBSD The Power to Serve silver trophy pippo's Avatar
    Join Date
    Jul 2001
    Location
    Italy
    Posts
    4,514
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    >> What am I missing here?
    Nothing,
    we need to change the rules as:

    RewriteEngine On
    RewriteRule ^albums(/([^/]+))?/?$ /visuals.php?album=$2 [L]

    it will only accept:
    albums ( album query var is empty )
    albums/ ( album query var is empty )
    albums/something ( album query var is something )
    albums/something/ ( album query var is something )
    which is what you wanted :-)

    so when the album query var is empty you can display a synopsis of all the albums.


    :-) pippo
    Mr Andrea
    Former Hosting Team Advisor
    Former Advisor of '03


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
  •