
{"id":2461,"date":"2017-12-10T01:57:07","date_gmt":"2017-12-10T06:57:07","guid":{"rendered":"https:\/\/ikriv.com\/blog\/?p=2461"},"modified":"2017-12-10T01:57:07","modified_gmt":"2017-12-10T06:57:07","slug":"insane-apache-rewriterules","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=2461","title":{"rendered":"Insane Apache RewriteRules"},"content":{"rendered":"<p>I have just spent a few fun hours fighting Apache rewrite rules in an <code>.htaccess<\/code> file. It turns out that temporary redirects just work, but permanent redirects require <code>RewriteBase<\/code>! Frankly, this is insane.<\/p>\n<p>Suppose, I have my web site <code>test.ikriv.com<\/code> that lives in <code>\/var\/www\/html<\/code> on the server disk. Let&#8217;s say I have the following contents in <code>\/var\/www\/html\/.htaccess<\/code>:<\/p>\n<pre>\r\nRewriteEngine on\r\nRewriteRule ^(.*)\\.temp$ $1.html\r\nRewriteRule ^(.*)\\.perm$ $1.html [R=permanent]<\/pre>\n<p>If type in the browser <code>https:\/\/test.ikriv.com\/file.temp<\/code>, I see the contents of <code>\/var\/www\/html\/file.html<\/code>, as expected. The server does not report the redirect back to the browser, but quietly serves <code>file.html<\/code> instead of <code>file.temp<\/code>. <\/p>\n<p>If, however, I type <code>https:\/\/test.ikriv.com\/file.perm<\/code> I get redirected to <code>https:\/\/test.ikriv.com\/var\/www\/html\/file.html<\/code> and then get error 404, telling me such file is not available.<\/p>\n<p>The root cause seems to be that in both cases <code>mod_rewrite<\/code> converts the incoming URL into <code>var\/www\/html\/file.html<\/code>. In the first case it just serves this file, which works great, but in the second case it returns the file path <b>to the browser<\/b>, who interprets that as a URL, and it all goes downhill from there. To fix the problem, I must add <code>RewriteBase<\/code> to the <code>.htaccess<\/code> file. To be fair, <a href=\"http:\/\/httpd.apache.org\/docs\/current\/mod\/mod_rewrite.html#rewritebase\">Apache docs do say it is required<\/a>, but they don&#8217;t say it is required only for permanent redirects. Most sane people would assume that if temporary redirects work, permanent redirects will work as well, but it is not the case.<\/p>\n<pre>\r\nRewriteEngine on\r\nRewriteBase \/\r\nRewriteRule ^(.*)\\.temp$ $1.html\r\nRewriteRule ^(.*)\\.perm$ $1.html [R=permanent]<\/pre>\n<p>Note that I need to specify the base URL of the directory. I.e., if I have <code>.htaccess<\/code> file in some other directory, I must add <code>RewriteBase \/that\/dir\/url<\/code>. This is quite inconvenient, because <\/p>\n<p>a) as I move directories around, <code>RewriteBase<\/code> may become broken, and<br \/>\nb) if a directory can be reached via multiple URLs, I will have to pick one as <code>RewriteBase<\/code>, with possible security issues and other inconveniences.<\/p>\n<p>Why can&#8217;t we just make things work without surprises? That&#8217;s obviously, a rhetoric question: one&#8217;s person surprise is another person completely obvious and logical behavior. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have just spent a few fun hours fighting Apache rewrite rules in an .htaccess file. It turns out that temporary redirects just work, but permanent redirects require RewriteBase! Frankly, <a href=\"https:\/\/ikriv.com\/blog\/?p=2461\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[4],"tags":[],"class_list":["entry","author-ikriv","post-2461","post","type-post","status-publish","format-standard","category-hack"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2461","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2461"}],"version-history":[{"count":7,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2461\/revisions"}],"predecessor-version":[{"id":2468,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2461\/revisions\/2468"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2461"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}