I've got a mod rewrite that I'm trying to do. Currently my .htaccess file looks like this:

RewriteRule ^videochannel/([^/\.]+)/?$ /video/viewchannel.php?curlname=$1 [L]

Which corresponds to the url:
/videochannel/videochannelname

I need to expand upon this. I need to add the variable so that the url for the rewrite reads: /video/viewchannel.php?curlname=$1&pagenumber=$2

Which would correspond to the url:
/videochannel/videochannelname/1

Here is the trick that I can't seem to figure out. Both the urls:
/videochannel/videochannelname/

and

/videochannel/videochannelname/1
need to work.

I've tried changing the mod rewrite to:

RewriteRule ^videochannel/([^/\.]+)/([^/\.]+)/?$ /video/viewchannel-testing.php?curlname=$1&pagenumber=$2 [L]

The works for:
/videochannel/videochannelname/1
but
/videochannel/videochannelname/
gives me a 404.

Any ideas?

    I've taken a lesson from drupal and handle it like this:

    Map the WHOLE url to a single page. That page figures out what to do and includes the necessary files.

      # Rewrite current-style URLs of the form 'index.php?q=x'.
      # but only for files that do not exist
      RewriteCond %{REQUEST_FILENAME} !-f
      # this is saying if it ends in a trailing slash, we don't want it
      # you might have to adjust this line to your tastes
      RewriteCond %{REQUEST_URI} !\/$
      # and the requested URI is not an existing directory
      RewriteCond %{REQUEST_FILENAME} !-d
      # finally we get to the rewrite, its about time!
      RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
    

    So /videochannel/videochannelname/ is sent in this case to index.php as

    index.php?q=/videochannel/videochannelname/

    and the other would come in as

    index.php?q=/videochannel/videochannelname/1

    Then index.php examines the URL and figures out what to do. This works exceedingly well.

      That's always a way of doing it, because it certainly makes it easier for those who are familiar to PHP but not with the apache redirect rules (and regex etc)

      However, I don't believe this problem requires such an urgent redesign 😉

      I think the problem here is that you're telling apache that you need to to have 1 or more after the last comma with the + character. Change this to * to mean 0 or more, and we should have a winner.

      RewriteRule ^videochannel/([^/\.]+)/([^/\.]*)/?$ /video/viewchannel-testing.php?curlname=$1&pagenumber=$2 [L]
      

      (You could alternatively make the group optional using a question mark, if this way doesn't work, try that)

        madwormer2,
        I tried your way and it seems to be working:

        RewriteRule ^testchannel/([^/\.]*)/([^/\.]*)/?$ /video/viewchannel-testing.php?curlname=$1&pagenumber=$2 [L]

        The only problem that I am now encountering is when that:

        /videochannel/videochannelnamehere/

        works as well as
        videochannel/videochannelnamehere/2

        and
        videochannel/videochannelnamehere/2/

        works, however

        /videochannel/videochannelnamehere

        without the "/" at the end does not work, it 404's.

        I don't know what you mean when you say "You could alternatively make the group optional using a question mark" It sounds like that would work, but I'm not sure how to implement it.

        Any suggestions?

          No need, just add a question mark after the other forward slash, and that should do it.

            4 days later

            Thanks for your help. That fixed it.

              Write a Reply...