Forum Moderators: phranque

Message Too Old, No Replies

Dynamic .htaccess

         

brandon0401

5:04 am on Feb 11, 2008 (gmt 0)

10+ Year Member



I am trying to make the site dynamic based on two parameters:

RewriteRule ^(.*)/(.*)$ /category.php?type=$1&query=$2 [L]

www.url.com/producer/him

but when I put that in,

none of my dir work, such as
/images

so how can I get around this without putting a word indicator in url or typing out every option in htaccess?

Thanks.

jdMorgan

3:38 pm on Feb 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here are three ways to do it:

  • Use a "flag" in the url itself: /cats/<category>/<query>
    This is by far the most efficient. You can use "/cats/," "/categories/," or just "/c/" -- It doesn't matter, as long as it's unique on your site.

  • Add exclusions for known and existing files and subdirectories using RewriteConds. Exclude /images, /stats, /admin, /robots.txt, /labels.rdf, /w3c/p3p.xml, etc. This is fairly efficient, but requires on-going maintenance if new top-level subdirectories and/or files are constantly being added.

  • Check for "file exists" and "directory exists" using RewriteConds. Then exclude requests for these 'real' files and directories from being rewritten. This requires the server to call the operating system and have it check the disk for the existence of a 'real' file, such as an image. Then it must call again to check for a directory of the same name. Because doing so involves executing thousands of additional CPU instructions and may involve actual disk reads, this method is the least efficient -- Thousands of blog sites run slowly because the default code shipped with the blog installer uses this method.

    As a side note, do not use multiple ".*" patterns in a RewriteCond or RewriteRule. Processing the ambiguous, greedy, and promiscuous ".*" pattern leads to multiple "back-off-and-retry" cycles as the regex matching engine tries to find a match. Instead, use a negative-match pattern, which allows the requested URL-path to be matched with the pattern in a single left-to-right pass:


    RewriteRule ^([^/]+)/([^/]+)$ /category.php?type=$1&query=$2 [L]

    This will match only /<directory>/<file> URL-paths. It will not match if the requested URL-path has more than one directory level, or if it ends with a slash. It will not match if there are no characters between the slashes. It will run dozens, hundreds, or thousands of time faster than ^(.*)/(.*)$ -- depending on the length of the requested URL-path. And using three or more consecutive ".*" patterns is far, far worse.

    Jim

  • brandon0401

    8:51 pm on Feb 11, 2008 (gmt 0)

    10+ Year Member



    Thanks for great reply, sounds like exclusions should be best, ive tried to redirect images, how would I do a exclusions, im not sure of that method, thanks again!
    your new method works fine, but still cutting out images/

    there will def be dir that can be excluded and should be easy to maintain..

    brandon0401

    9:07 pm on Feb 11, 2008 (gmt 0)

    10+ Year Member



    thanks just fyi

    RewriteRule ^(images) - [L]

    worked..

    jdMorgan

    9:29 pm on Feb 11, 2008 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    An alternate form:

    RewriteCond %{REQUEST_URI} !^/images/
    RewriteRule ^([^/]+)/([^/]+)$ /category.php?type=$1&query=$2 [L]

    Jim