Hey guys, I created a .htaccess with the following rules to clean up urls:

RewriteEngine ON
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^waiting/(.*)/(.*)$ index.php?action=waiting&kid=$1&name=$2 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^waiting/(.*)$ index.php?action=waiting&page=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^photos/(.*)$ index.php?action=photos&group=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^view/(.*)$ view.php?image=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?action=$1 [L]

I'm just wondering is there a way to accomplish the same result, without having to put the conditions before each rule?

    Untested theory, but perhaps you could place something like this rule first:

    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule (.+) $1 [L]

    and then omit the Cond's from the rest of your rules.

    Basically, it's my feeble attempt and flipping the logic around; rather than making all of your rules catch the requests that don't match a real file/directory, the above rule would first check to see if the request does match a valid file or directory and stop the rewriting process short right there (by rewriting the request to... itself... and making that the [L]ast rule processed).

      That works perfectly, however I'm having a problem with:
      RewriteRule view/(.*)$ view.php?image=$1 [L]
      example.com/view.php?image=2 works but
      example.com/view/2 is not working.. its acting like the get array is empty. However the other rules are working perfectly... thoughts?

        What does this:

        var_dump($_GET);

        produce if you put it at the top of view.php and attempt to use the second URL?

          ok actually...

          RewriteEngine ON
          RewriteBase /
          
          RewriteCond %{REQUEST_FILENAME} -f [OR]
          RewriteCond %{REQUEST_FILENAME} -d
          RewriteRule (.+) $1 [L]
          
          RewriteRule ^waiting/(.*)/(.*)$ index.php?action=waiting&kid=$1&name=$2 [L]
          RewriteRule ^waiting/(.*)$ index.php?action=waiting&page=$1 [L]
          RewriteRule ^photos/(.*)$ index.php?action=photos&group=$1 [L]
          RewriteRule ^view/(.*)$ view.php?image=$1 [L]
          RewriteRule ^(.*)$ index.php?action=$1 [L]

          Works except for the view line, which results in a 500 internal server error. However,

          RewriteEngine ON
          RewriteBase /
          
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteCond %{REQUEST_FILENAME} !-d
          RewriteRule ^waiting/(.*)/(.*)$ index.php?action=waiting&kid=$1&name=$2 [L]
          
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteCond %{REQUEST_FILENAME} !-d
          RewriteRule ^waiting/(.*)$ index.php?action=waiting&page=$1 [L]
          
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteCond %{REQUEST_FILENAME} !-d
          RewriteRule ^photos/(.*)$ index.php?action=photos&group=$1 [L]
          
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteCond %{REQUEST_FILENAME} !-d
          RewriteRule ^view/(.*)$ view.php?image=$1 [L]
          
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteCond %{REQUEST_FILENAME} !-d
          RewriteRule ^(.*)$ index.php?action=$1 [L]

          all works, and view.php loads with an empty $_GET array (I have error checking in the script to know that). I am confused why the line partially works in one and doesn't work at all in the other.

            Derokorian;10983459 wrote:

            ok actually...

            RewriteEngine ON
            RewriteBase /
            
            RewriteCond %{REQUEST_FILENAME} -f [OR]
            RewriteCond %{REQUEST_FILENAME} -d
            RewriteRule (.+) $1 [L]
            
            RewriteRule ^waiting/(.*)/(.*)$ index.php?action=waiting&kid=$1&name=$2 [L]
            RewriteRule ^waiting/(.*)$ index.php?action=waiting&page=$1 [L]
            RewriteRule ^photos/(.*)$ index.php?action=photos&group=$1 [L]
            RewriteRule ^view/(.*)$ view.php?image=$1 [L]
            RewriteRule ^(.*)$ index.php?action=$1 [L]

            Works except for the view line, which results in a 500 internal server error.

            Works just fine for me; pasted that into my .htaccess file, went to "localhost/view/5", and got this:

            array(1) { ["image"]=> string(1) "5" }

            So... look at the Apache error logs to figure out what's going on, as it's not a problem with that htaccess code.

              So I changed view from being stand alone to being included like all the other pages ie: index.php?action=view&image=$1 and its working now.. maybe I had a typo that I couldn't seem to fix? I dunno its all working perfectly now and in much less lines! Thanks so much for the help brad =D

                Write a Reply...