{"id":115,"date":"2019-04-10T23:39:18","date_gmt":"2019-04-10T20:39:18","guid":{"rendered":"https:\/\/lyubomir-angelov.com\/?p=115"},"modified":"2019-04-11T20:36:06","modified_gmt":"2019-04-11T17:36:06","slug":"apache-rewrite-all-uris-to-a-single-file","status":"publish","type":"post","link":"https:\/\/lyubomir-angelov.com\/blog\/apache-rewrite-all-uris-to-a-single-file\/","title":{"rendered":"Apache &#8211; Rewrite all URIs to a single file"},"content":{"rendered":"\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lyubomir-angelov.com\/blog\/wp-content\/uploads\/2019\/04\/Apache-FallbackResource.jpg\" alt=\"\" class=\"wp-image-121\" width=\"597\" height=\"448\" srcset=\"https:\/\/lyubomir-angelov.com\/blog\/wp-content\/uploads\/2019\/04\/Apache-FallbackResource.jpg 1000w, https:\/\/lyubomir-angelov.com\/blog\/wp-content\/uploads\/2019\/04\/Apache-FallbackResource-300x225.jpg 300w, https:\/\/lyubomir-angelov.com\/blog\/wp-content\/uploads\/2019\/04\/Apache-FallbackResource-768x576.jpg 768w\" sizes=\"auto, (max-width: 597px) 85vw, 597px\" \/><\/figure>\n\n\n<p>This week I had to help a friend with his new small ReacJS website running on Apache.<\/p>\n<p>His problem was that URIs which does not exist the Apache was returning as 404 pages, he needed a common rule used by a lot of PHP frameworks, Rewrite all URIs which are not Files or Directories to <strong>\/index.html<\/strong><\/p>\n<p>Example URL <code>www.example.com\/myprofile<\/code> to actually load <code>www.example.com\/index.html<\/code> and let the ReactJS route do his work and show what it needs.<\/p>\n<p><!--more--><\/p>\n<p>In that case, you might write something like this:<\/p>\n<pre><code class=\"bash language-bash\">RewriteEngine on\n\nRewriteCond %{REQUEST_FILENAME} !-f\nRewriteCond %{REQUEST_FILENAME} !-d\nRewriteRule ^ index.html [L]\n<\/code><\/pre>\n<p>What is this doing, it will check the URI either exists as a file or is a directory, if it is NOT, it will call <strong>index.html<\/strong>. The [L] means that this will be the LAST match and will stop there, will not continue if we have other rules below that.<\/p>\n<p>This is code is ok, not so simple and you can mess up the conditions, for example, I have seen a lot of [OR] next to <code>RewriteCond %{REQUEST_FILENAME} !-f [OR]<\/code> which will not work in our case above.<\/p>\n<p>If you need this simple Rewrite rule, you might use Apache command available after <strong>2.2.16 version<\/strong><\/p>\n<pre><code class=\"bash language-bash\">FallbackResource \/index.html\n<\/code><\/pre>\n<p>This will do the same as the above 4 lines if the URI is not found on the server, ie. not a File or a Dir, it will load to <strong>\/index.html<\/strong><\/p>\n<p>This command is not part of <strong>mod<em>rewrite<\/em><\/strong><em>, but of <strong>mod<\/strong><\/em><strong>dir<\/strong>, if you don&#8217;t have it enabled, run <code>sudo a2enmod dir<\/code> and restart the Apache.<\/p>","protected":false},"excerpt":{"rendered":"<p>This week I had to help a friend with his new small ReacJS website running on Apache. His problem was that URIs which does not exist the Apache was returning as 404 pages, he needed a common rule used by a lot of PHP frameworks, Rewrite all URIs which are not Files or Directories to &hellip; <a href=\"https:\/\/lyubomir-angelov.com\/blog\/apache-rewrite-all-uris-to-a-single-file\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Apache &#8211; Rewrite all URIs to a single file&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[20,22,21],"class_list":["post-115","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-apache","tag-htaccess","tag-rewrite"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Apache - Rewrite all URIs to a single file - Lyubomir Angelov<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/lyubomir-angelov.com\/blog\/apache-rewrite-all-uris-to-a-single-file\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Apache - Rewrite all URIs to a single file - Lyubomir Angelov\" \/>\n<meta property=\"og:description\" content=\"This week I had to help a friend with his new small ReacJS website running on Apache. His problem was that URIs which does not exist the Apache was returning as 404 pages, he needed a common rule used by a lot of PHP frameworks, Rewrite all URIs which are not Files or Directories to &hellip; Continue reading &quot;Apache &#8211; Rewrite all URIs to a single file&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/lyubomir-angelov.com\/blog\/apache-rewrite-all-uris-to-a-single-file\/\" \/>\n<meta property=\"og:site_name\" content=\"Lyubomir Angelov\" \/>\n<meta property=\"article:published_time\" content=\"2019-04-10T20:39:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-04-11T17:36:06+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/lyubomir-angelov.com\/blog\/wp-content\/uploads\/2019\/04\/Apache-FallbackResource.jpg\" \/>\n<meta name=\"author\" content=\"Lyubomir\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@angelovcode\" \/>\n<meta name=\"twitter:site\" content=\"@angelovcode\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Lyubomir\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/lyubomir-angelov.com\/blog\/apache-rewrite-all-uris-to-a-single-file\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/lyubomir-angelov.com\/blog\/apache-rewrite-all-uris-to-a-single-file\/\"},\"author\":{\"name\":\"Lyubomir\",\"@id\":\"https:\/\/lyubomir-angelov.com\/blog\/#\/schema\/person\/5e091f5c2fa79f57d30d482a7ef8cd92\"},\"headline\":\"Apache &#8211; Rewrite all URIs to a single file\",\"datePublished\":\"2019-04-10T20:39:18+00:00\",\"dateModified\":\"2019-04-11T17:36:06+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/lyubomir-angelov.com\/blog\/apache-rewrite-all-uris-to-a-single-file\/\"},\"wordCount\":246,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/lyubomir-angelov.com\/blog\/#\/schema\/person\/5e091f5c2fa79f57d30d482a7ef8cd92\"},\"keywords\":[\"apache\",\"htaccess\",\"rewrite\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/lyubomir-angelov.com\/blog\/apache-rewrite-all-uris-to-a-single-file\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/lyubomir-angelov.com\/blog\/apache-rewrite-all-uris-to-a-single-file\/\",\"url\":\"https:\/\/lyubomir-angelov.com\/blog\/apache-rewrite-all-uris-to-a-single-file\/\",\"name\":\"Apache - Rewrite all URIs to a single file - Lyubomir Angelov\",\"isPartOf\":{\"@id\":\"https:\/\/lyubomir-angelov.com\/blog\/#website\"},\"datePublished\":\"2019-04-10T20:39:18+00:00\",\"dateModified\":\"2019-04-11T17:36:06+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/lyubomir-angelov.com\/blog\/apache-rewrite-all-uris-to-a-single-file\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/lyubomir-angelov.com\/blog\/apache-rewrite-all-uris-to-a-single-file\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/lyubomir-angelov.com\/blog\/apache-rewrite-all-uris-to-a-single-file\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/lyubomir-angelov.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Apache &#8211; Rewrite all URIs to a single file\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/lyubomir-angelov.com\/blog\/#website\",\"url\":\"https:\/\/lyubomir-angelov.com\/blog\/\",\"name\":\"Lyubomir Angelov\",\"description\":\"The life of a Manager\",\"publisher\":{\"@id\":\"https:\/\/lyubomir-angelov.com\/blog\/#\/schema\/person\/5e091f5c2fa79f57d30d482a7ef8cd92\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/lyubomir-angelov.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/lyubomir-angelov.com\/blog\/#\/schema\/person\/5e091f5c2fa79f57d30d482a7ef8cd92\",\"name\":\"Lyubomir\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/lyubomir-angelov.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/f6b8c15868355fa857fad18647aeadd07814e3d9baa72a1c92e718056684fec7?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/f6b8c15868355fa857fad18647aeadd07814e3d9baa72a1c92e718056684fec7?s=96&d=mm&r=g\",\"caption\":\"Lyubomir\"},\"logo\":{\"@id\":\"https:\/\/lyubomir-angelov.com\/blog\/#\/schema\/person\/image\/\"},\"sameAs\":[\"https:\/\/twitter.com\/angelovcode\"],\"url\":\"https:\/\/lyubomir-angelov.com\/blog\/author\/superfly\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Apache - Rewrite all URIs to a single file - Lyubomir Angelov","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/lyubomir-angelov.com\/blog\/apache-rewrite-all-uris-to-a-single-file\/","og_locale":"en_US","og_type":"article","og_title":"Apache - Rewrite all URIs to a single file - Lyubomir Angelov","og_description":"This week I had to help a friend with his new small ReacJS website running on Apache. His problem was that URIs which does not exist the Apache was returning as 404 pages, he needed a common rule used by a lot of PHP frameworks, Rewrite all URIs which are not Files or Directories to &hellip; Continue reading \"Apache &#8211; Rewrite all URIs to a single file\"","og_url":"https:\/\/lyubomir-angelov.com\/blog\/apache-rewrite-all-uris-to-a-single-file\/","og_site_name":"Lyubomir Angelov","article_published_time":"2019-04-10T20:39:18+00:00","article_modified_time":"2019-04-11T17:36:06+00:00","og_image":[{"url":"https:\/\/lyubomir-angelov.com\/blog\/wp-content\/uploads\/2019\/04\/Apache-FallbackResource.jpg"}],"author":"Lyubomir","twitter_card":"summary_large_image","twitter_creator":"@angelovcode","twitter_site":"@angelovcode","twitter_misc":{"Written by":"Lyubomir","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/lyubomir-angelov.com\/blog\/apache-rewrite-all-uris-to-a-single-file\/#article","isPartOf":{"@id":"https:\/\/lyubomir-angelov.com\/blog\/apache-rewrite-all-uris-to-a-single-file\/"},"author":{"name":"Lyubomir","@id":"https:\/\/lyubomir-angelov.com\/blog\/#\/schema\/person\/5e091f5c2fa79f57d30d482a7ef8cd92"},"headline":"Apache &#8211; Rewrite all URIs to a single file","datePublished":"2019-04-10T20:39:18+00:00","dateModified":"2019-04-11T17:36:06+00:00","mainEntityOfPage":{"@id":"https:\/\/lyubomir-angelov.com\/blog\/apache-rewrite-all-uris-to-a-single-file\/"},"wordCount":246,"commentCount":0,"publisher":{"@id":"https:\/\/lyubomir-angelov.com\/blog\/#\/schema\/person\/5e091f5c2fa79f57d30d482a7ef8cd92"},"keywords":["apache","htaccess","rewrite"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/lyubomir-angelov.com\/blog\/apache-rewrite-all-uris-to-a-single-file\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/lyubomir-angelov.com\/blog\/apache-rewrite-all-uris-to-a-single-file\/","url":"https:\/\/lyubomir-angelov.com\/blog\/apache-rewrite-all-uris-to-a-single-file\/","name":"Apache - Rewrite all URIs to a single file - Lyubomir Angelov","isPartOf":{"@id":"https:\/\/lyubomir-angelov.com\/blog\/#website"},"datePublished":"2019-04-10T20:39:18+00:00","dateModified":"2019-04-11T17:36:06+00:00","breadcrumb":{"@id":"https:\/\/lyubomir-angelov.com\/blog\/apache-rewrite-all-uris-to-a-single-file\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/lyubomir-angelov.com\/blog\/apache-rewrite-all-uris-to-a-single-file\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/lyubomir-angelov.com\/blog\/apache-rewrite-all-uris-to-a-single-file\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/lyubomir-angelov.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Apache &#8211; Rewrite all URIs to a single file"}]},{"@type":"WebSite","@id":"https:\/\/lyubomir-angelov.com\/blog\/#website","url":"https:\/\/lyubomir-angelov.com\/blog\/","name":"Lyubomir Angelov","description":"The life of a Manager","publisher":{"@id":"https:\/\/lyubomir-angelov.com\/blog\/#\/schema\/person\/5e091f5c2fa79f57d30d482a7ef8cd92"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/lyubomir-angelov.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/lyubomir-angelov.com\/blog\/#\/schema\/person\/5e091f5c2fa79f57d30d482a7ef8cd92","name":"Lyubomir","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/lyubomir-angelov.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/f6b8c15868355fa857fad18647aeadd07814e3d9baa72a1c92e718056684fec7?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f6b8c15868355fa857fad18647aeadd07814e3d9baa72a1c92e718056684fec7?s=96&d=mm&r=g","caption":"Lyubomir"},"logo":{"@id":"https:\/\/lyubomir-angelov.com\/blog\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/twitter.com\/angelovcode"],"url":"https:\/\/lyubomir-angelov.com\/blog\/author\/superfly\/"}]}},"_links":{"self":[{"href":"https:\/\/lyubomir-angelov.com\/blog\/wp-json\/wp\/v2\/posts\/115","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lyubomir-angelov.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lyubomir-angelov.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lyubomir-angelov.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lyubomir-angelov.com\/blog\/wp-json\/wp\/v2\/comments?post=115"}],"version-history":[{"count":6,"href":"https:\/\/lyubomir-angelov.com\/blog\/wp-json\/wp\/v2\/posts\/115\/revisions"}],"predecessor-version":[{"id":127,"href":"https:\/\/lyubomir-angelov.com\/blog\/wp-json\/wp\/v2\/posts\/115\/revisions\/127"}],"wp:attachment":[{"href":"https:\/\/lyubomir-angelov.com\/blog\/wp-json\/wp\/v2\/media?parent=115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lyubomir-angelov.com\/blog\/wp-json\/wp\/v2\/categories?post=115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lyubomir-angelov.com\/blog\/wp-json\/wp\/v2\/tags?post=115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}