Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jul 2010
    Location
    St George, UT
    Posts
    138
    Thanks
    6
    Thanked 17 Times in 17 Posts

    .htaccess - subdirectories -> variables and trailing slash

    My /browse script does all the heavy lifting for handling categories (and pagination), and recipes. Recipes don't use any type of pagination, but categories do.

    /browse/recipe/this-is-a-recipe becomes
    /browse/index.php?recipe=this-is-a-recipe

    /browse/category/All becomes
    /browse/index.php?cat=All

    /browse/category/All/page/2/ becomes
    /browse/index.php?cat=All&page=2

    Using the following rewrite, this WORKS

    Code:
    RewriteEngine on
    RewriteRule ^recipe/(.*)$ index.php?recipe=$1
    RewriteRule ^category/([^/]+)/page/([^/]+) index.php?cat=$1&page=$2
    RewriteRule ^category/(.*)$ index.php?cat=$1
    However... If I do

    /browse/category/All/ or
    /browse/category/page/2

    It fails. The trailing slash is giving me a problem and I'm not sure how to write all conditions to ignore the trailing slash. I also don't want two URL's so I'd like /browse/category/All to be rewritten as /browse/category/All/.

    Any suggestions?

    http://www.whatstoeat.co/browse/

  • #2
    Regular Coder
    Join Date
    Feb 2009
    Location
    Wheeling, IL
    Posts
    358
    Thanks
    5
    Thanked 62 Times in 60 Posts
    There is a relatively easy solution. Actually, there are two, but I will address the one that doesn't require you to add about 3-4 lines to your redirects.

    Add a trailing slash at the end of each of your matching rules and make it optional (With a following question mark) like so:

    Code:
    RewriteEngine on
    RewriteRule ^recipe/(.*)/?$ index.php?recipe=$1
    RewriteRule ^category/([^/]+)/page/([^/]+)/?$ index.php?cat=$1&page=$2
    RewriteRule ^category/(.*)/?$ index.php?cat=$1
    Enjoy.
    The way to success is to assume that there are no impossible things. After all, if you think something is impossible, you will not even try to do it.

    How to ask smart questions?

  • Users who have thanked Eldarrion for this post:

    wildreason (08-25-2010)

  • #3
    Regular Coder
    Join Date
    Jul 2010
    Location
    St George, UT
    Posts
    138
    Thanks
    6
    Thanked 17 Times in 17 Posts
    Thanks... so a ? after the character indicates it is optional?

  • #4
    New Coder
    Join Date
    Aug 2010
    Posts
    11
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by wildreason View Post
    Thanks... so a ? after the character indicates it is optional?
    That is correct, a question mark after a character makes it optional


  •  

    Posting Permissions

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