Forum Moderators: phranque
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?
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]
# 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]
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