Forum Moderators: phranque
# backward compatibility ruleset for
# rewriting document.html to document.shtml
# when and only when document.shtml exists
# but no longer document.html
RewriteEngine on
RewriteBase /~quux/
# parse out basename, but remember the fact
RewriteRule ^(.*)\.html$ $1 [C,E=WasHTML:yes]
# rewrite to document.shtml if exists
RewriteCond %{REQUEST_FILENAME}.shtml -f
RewriteRule ^(.*)$ $1.shtml [S=1]
# else reverse the previous basename cutout
RewriteCond %{ENV:WasHTML} ^yes$
RewriteRule ^(.*)$ $1.html
This works great for pages in the main directory, but subdirectory pages are still going to the 404error page and not being redirected. Since regular expression are making my head spin, can anyone point the way to a resolution? Thanks.
You can try rooting the substitution URL-path by preceding it with a slash and see if that helps:
RewriteRule ^(.+)\.html$ $1 [C,E=WasHTML:yes]
RewriteCond %{REQUEST_FILENAME}.shtml -f
RewriteRule ^(.*)$ /$1.shtml [S=1]
RewriteCond %{ENV:WasHTML} ^yes$
RewriteRule ^(.*)$ /$1.html
RewriteRule ^(.+)\.html$ $1 [C,E=WasHTML:yes]
RewriteCond %{REQUEST_FILENAME}.shtml -f
RewriteRule ^(.*)$ /$1.shtml [L]
RewriteCond %{ENV:WasHTML} ^yes$
RewriteRule ^(.*)$ /$1.html [L]
Jim
On a separate but related topic, are there any newbie-friendly tutorials on regular expressions? Every one I've looked at so far makes my eyes start crossing after a few pages. ;)
Thanks again.
jdkuehne
One thing you'll notice as you deal more with regex is that it is hard to read, but it's much, much easier to write, once you get comfortable with it. Regular expressions are so powerful, and each little nuance so important, that it's difficult to comprehend the "side effects" of a particular complex pattern unless you know exactly what the original goal was. However, if you set out with a clear goal in mind, then it gets quite easy to write the regular expressions pattern needed to reach that goal... Well, it's easy most of the time. ;)
Jim