{"id":5086,"date":"2016-08-27T10:00:17","date_gmt":"2016-08-27T14:00:17","guid":{"rendered":"http:\/\/www.dr-chuck.com\/csev-blog\/?p=5086"},"modified":"2016-08-28T19:43:05","modified_gmt":"2016-08-28T23:43:05","slug":"dynamic-htaccess-to-deal-with-url-rewriting-mod_rewrite-c-and-fallbackresource","status":"publish","type":"post","link":"https:\/\/www.dr-chuck.com\/csev-blog\/2016\/08\/dynamic-htaccess-to-deal-with-url-rewriting-mod_rewrite-c-and-fallbackresource\/","title":{"rendered":"Dynamic .htaccess to deal with Url Rewriting mod_rewrite.c, and FallbackResource"},"content":{"rendered":"<p>As I built <a href=\"http:\/\/www.tsugi.org\/\" target=\"_blank\">Tsugi<\/a>, I want to ship with a decent, working <code>.htaccess<\/code> in folders that need it.   My most typical use case is that I want to map all the URLs in a folder into a file like <code>index.php<\/code>.<\/p>\n<p>There are two good ways to do this.  The old standby is a long set of <code>mod_rewrite<\/code> rules.  The new, much more elegant trick is to use <code>FallbackResource<\/code> in <code>mod_dir<\/code> in later versions of Apache 2.2.<\/p>\n<p>The problem is that clever hosting providers upgrade to the new Apache and then figure they can remove <code>mod_rewrite<\/code> so you know how to do it in either case but don&#8217;t have a good way to trigger when to use what approach.<\/p>\n<p>This is my approach that I use in Tsugi when I want to map all URLs to one file:<\/p>\n<pre>\r\n    &amp;lt;IfModule mod_rewrite.c&gt;\r\n        RewriteEngine on\r\n        RewriteRule ^ - [E=protossl]\r\n        RewriteCond %{HTTPS} on\r\n        RewriteRule ^ - [E=protossl:s]\r\n        RewriteRule \"(^|\/)\\.\" - [F]\r\n        RewriteCond %{REQUEST_FILENAME} !-f\r\n        RewriteCond %{REQUEST_FILENAME} !-d\r\n        RewriteCond %{REQUEST_URI} !=\/favicon.ico\r\n        RewriteRule ^ index.php [L]\r\n    &amp;lt;\/IfModule&gt;\r\n    \r\n    &amp;lt;IfModule !mod_rewrite.c&gt;\r\n        FallbackResource index.php\r\n    &amp;lt;\/IfModule&gt;\r\n<\/pre>\n<p>It is not perfect but kind of deals with things as the move forward.  If <code>mod_rewrite<\/code> is there &#8211; use it &#8211; it works in later Apache versions as well but if <code>mod_rewrite<\/code> is there, use it and if not, hope that <code>FallbackResource<\/code> is there.<\/p>\n<p>Now of course there are some Apache versions \/ setups where this fails &#8211; but on average, over time as Apache&#8217;s get upgraded, things get simpler and over time the <code>mod_rewrite<\/code> code just will stop activating.<\/p>\n<p>I also added this information to a <a href=\"http:\/\/stackoverflow.com\/questions\/10707186\/detect-apache-version-in-apache-config\/39181841#39181841\" target=\"_blank\">Stack Overflow question<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As I built Tsugi, I want to ship with a decent, working .htaccess in folders that need it. My most typical use case is that I want to map all the URLs in a folder into a file like index.php. There are two good ways to do this. The old standby is a long set [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5086","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/posts\/5086","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/comments?post=5086"}],"version-history":[{"count":9,"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/posts\/5086\/revisions"}],"predecessor-version":[{"id":5097,"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/posts\/5086\/revisions\/5097"}],"wp:attachment":[{"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/media?parent=5086"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/categories?post=5086"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/tags?post=5086"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}