Forum Moderators: phranque

Message Too Old, No Replies

mod rerwite question

how to exclude specific static pages from mod rerwite

         

omoutop

8:19 am on May 31, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi all and thanks in advance for any reply...

Lets say we have this rerwite rule in htaccess..
RewriteRule ^([^/]+)/([^/]+)/([^-]+)-(*.)\.htm$ page.php?var1=$1&var2=$2&var3=$3&var4=$4 [NC,L]

I also have the test1/test2/test3-test4.htm (a static page)

Since the pattern of my page match the one in htaccess, htaccess created page overides the static one and browser shows the created page.

Is it possible to exclude this url/page from the above rule?

jdMorgan

2:47 pm on May 31, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you cannot change your URL/directory layout, use RewriteCond to exclude the specific pages in one of two ways:

For a small list of pages, exclude them specifically, by matching %{REQUEST_URI}. This is efficient up to, say, 50 URLS.
For a large number of static pages, exclude them by checking for file-exists using %{REQUEST_FILENAME}. If the requested page does not exist as a file, rewrite it to the script. Otherwise, serve the static page.


# Exclude list of static pages
RewriteCond %{REQUEST_URI} !^/test1/test2/test3-test4\.htm$
RewriteCond %{REQUEST_URI} !^/test1/test2/test3-test5\.htm$
RewriteCond %{REQUEST_URI} !^/test1/test2/test3-test67\.htm$
# Rewrite others to script
RewriteRule ^([^/]+)/([^/]+)/([^-]+)-([^.]+)\.htm$ page.php?var1=$1&var2=$2&var3=$3&var4=$4 [NC,L]

-or-

# If requested page does not exist as a file
RewriteCond %{REQUEST_FILENAME} !-f
# rewrite the request to the script
RewriteRule ^([^/]+)/([^/]+)/([^-]+)-([^.]+)\.htm$ page.php?var1=$1&var2=$2&var3=$3&var4=$4 [NC,L]

It's important to note that althought the second ruleset is shorter, it invokes a search of your server's filesystem to check for existence of the file, and this adds considerable overhead. For that reason, the first 'brute force' approach is more efficient in many cases, as long as the number of URLs being checked is reasonable.

As noted above, for very large sites, the best approach is to re-arrange your URL/directory structure so that the path path itself will indicate that the pages is static or dynamic. For a trival (if ugly) example, you could simply put static and dyanmic pages in two directory paths:

/stat/test1/test2/test3-test4.htm
/dynm/test1/test2/test8-test9.htm

This makes mod_rewrite's job of determining which requests need to be rewrriten quite trivial -- and therefore, efficient.

Examine your current URL-structure; You may actually already have a way to determine static from dynamic pages using information encoded in the existing URLs. If so, then that information can be used by mod_rewrite instead of doing a case-by-case exclusion, a file-exists check, or having to rename your current URLs.

Jim

omoutop

6:01 am on Jun 1, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks jim for the extended analysis. The "brute" method should work (i need to exclude about 20 pages out of possible 1500 created ones!).

Thanks again